def test_clean_pycache_many_lvls_below_cwd(self, tmpdir, capfd, flask_app): """ run clean on the following test directory: <some-unique-tmpdir>/ shopyo/ shopyo/ mod/ box/ __pycache__/ file.pyc """ path = tmpdir.mkdir("shopyo").mkdir("shopyo").mkdir("mod").mkdir("box") pycache_path = path.mkdir("__pycache__") pyc = pycache_path.join("file.pyc") pyc.write("content") os.chdir(tmpdir) clean(flask_app) captured = capfd.readouterr() expected_out = ( "[x] all tables dropped\n" "[x] __pycache__ successfully deleted\n" ) expected_err_shopyo_db = ( f"[ ] unable to delete {os.path.join(tmpdir, 'shopyo.db')}" ) expected_err_migrations = ( f"[ ] unable to delete {os.path.join(tmpdir, 'migrations')}" ) assert os.path.exists(pycache_path) is False assert expected_out in captured.out assert expected_err_shopyo_db in captured.err assert expected_err_migrations in captured.err
def custom_commands(args): # non migration commands if args[1] != "db": if args[1] == "initialise": # auto import models autoload_models() initialise() elif args[1] == "clean": clean() elif args[1] == "runserver": runserver() elif args[1] == "rundebug": if len(args) == 2 + 1: rundebug(port=int(args[2])) else: rundebug() elif args[1] == "test": print("test ok") elif args[1] == "new" and args[2] and args[3]: # new <path> <folder name> new_project(args[2], args[3]) elif args[1] == 'startapp' and args[2]: create_module(args[2]) elif args[1] == 'populate': upload_all() elif args[1] == 'applysettings': apply_settings() sys.exit() elif args[1] == "db": autoload_models()
def test_clean_on_migration_folder(self, tmpdir, capfd, flask_app): """ run clean on the following test directory: <some-unique-tmpdir>/ migrations/ env.py alembic.ini """ migrations_path = tmpdir.mkdir("migrations") env = migrations_path.join("env.py") alembic = migrations_path.join("alembic.ini") env.write("content-env") alembic.write("content-alembic") os.chdir(tmpdir) clean(flask_app) captured = capfd.readouterr() expected_out = ( "[x] all tables dropped\n" f"[x] folder '{os.path.join(tmpdir, 'migrations')}' " "successfully deleted\n" ) expected_err_pycache = "[ ] __pycache__ doesn't exist\n" expected_err_shopyo_db = ( f"[ ] unable to delete {os.path.join(tmpdir, 'shopyo.db')}" ) assert os.path.exists(migrations_path) is False assert expected_out in captured.out assert expected_err_pycache in captured.err assert expected_err_shopyo_db in captured.err
def test_clean_on_shopyo_db_file(self, tmpdir, capfd, flask_app): """ run clean on the following test directory: <some-unique-tmpdir>/ shopyo.db """ shopyo_db = tmpdir.join("shopyo.db") shopyo_db.write("content") os.chdir(tmpdir) clean(flask_app) captured = capfd.readouterr() expected_out = ( "[x] all tables dropped\n" f"[x] file '{os.path.join(tmpdir, 'shopyo.db')}' " "successfully deleted\n" ) expected_err_pycache = "[ ] __pycache__ doesn't exist\n" expected_err_migrations = ( f"[ ] unable to delete {os.path.join(tmpdir, 'migrations')}" ) assert os.path.exists(shopyo_db) is False assert expected_out in captured.out assert expected_err_pycache in captured.err assert expected_err_migrations in captured.err
def test_clean_on_pycache_shopyo_migration(self, tmpdir, flask_app, capfd): """ run clean on the following test directory shopyo/ shopyo/ migrations/ alembic.ini env.py module1/ __pycache__/ file.pyc module2/ __pycache__/ file.pyc shopyo.db """ shopyo_path = tmpdir.mkdir("shopyo").mkdir("shopyo") migrations_path = shopyo_path.mkdir("migrations") env = migrations_path.join("env.py") env.write("content-env") alembic = migrations_path.join("alembic.ini") alembic.write("content-alembic") pycache_path1 = shopyo_path.mkdir("module1").mkdir("__pycache__") pycache_path2 = shopyo_path.mkdir("module2").mkdir("__pycache__") pyc1 = pycache_path1.join("file.pyc") pyc1.write("content") pyc2 = pycache_path2.join("file.pyc") pyc2.write("content") shopyo_db = shopyo_path.join("shopyo.db") shopyo_db.write("content") os.chdir(shopyo_path) clean(flask_app) captured = capfd.readouterr() expected_out = ( "[x] all tables dropped\n" "[x] __pycache__ successfully deleted\n" f"[x] file '{os.path.join(shopyo_path, 'shopyo.db')}' " "successfully deleted\n" f"[x] folder '{os.path.join(shopyo_path, 'migrations')}' " "successfully deleted\n" ) assert expected_out in captured.out assert os.path.exists(migrations_path) is False assert os.path.exists(pycache_path1) is False assert os.path.exists(pycache_path2) is False assert os.path.exists(shopyo_db) is False
def test_clean_on_no_files_to_clean(self, tmpdir, capfd, flask_app): os.chdir(tmpdir) clean(flask_app) captured = capfd.readouterr() expected_out = "[x] all tables dropped\n" expected_err_pycache = "[ ] __pycache__ doesn't exist\n" expected_err_shopyo_db = ( f"[ ] unable to delete {os.path.join(tmpdir, 'shopyo.db')}" ) expected_err_migrations = ( f"[ ] unable to delete {os.path.join(tmpdir, 'migrations')}" ) assert expected_out in captured.out assert expected_err_pycache in captured.err assert expected_err_shopyo_db in captured.err assert expected_err_migrations in captured.err
def process(args): printinfo() if args[0] == "initialise" or args[0] == "initialize": clean(app) autoload_models() initialise() elif args[0] == "clean": clean(app) elif args[0] == "runserver": runserver() elif args[0] == "rundebug": app.run(debug=True, host="0.0.0.0") try: if args[1]: app.run(debug=True, host="0.0.0.0", port=int(args[1])) except IndexError as e: raise e elif args[0] == "collectstatic": if len(args) == 1: collectstatic() elif len(args) == 2: collectstatic(target_module=args[1]) elif args[0] == "test": print("test ok") elif args[0] == "startapp" and args[1]: create_module(args[1]) elif args[0] == "startbox" and args[1]: create_box(args[1]) elif args[0] == "startsubapp" and args[1] and args[3]: if args[2].lower() == "in": create_module_in_box(args[1], args[3]) elif args[0] == "db": try: autoload_models() if args[1] == "migrate": subprocess.run(["flask", "db", "migrate"]) elif args[1] == "upgrade": subprocess.run(["flask", "db", "upgrade"]) elif args[1] == "init": subprocess.run(["flask", "db", "init"]) except IndexError as e: print("db requires more options") raise e else: print("Command not recognised")
def custom_commands(args): # non migration commands if args[1] != "db": if args[1] == "initialise": autoload_models() initialise() elif args[1] == "clean": clean() elif args[1] == "runserver": runserver() elif args[1] == "rundebug": rundebug() elif args[1] == "test": print("test ok") elif args[1] == 'startapp' and args[2]: create_module(args[2]) sys.exit() elif args[1] == "db": autoload_models()
def test_clean_many_pycache_in_nested_dirs(self, tmpdir, capfd, flask_app): """ run clean on the following test directory: <some-unique-tmpdir>/ __pycache__/ file.pyc shopyo/ __pycache__ file.pyc module/ __pycache__ file.pyc """ pycache_path1 = tmpdir.mkdir("__pycache__") pyc1 = pycache_path1.join("file.pyc") pyc1.write("content") shopyo_path = tmpdir.mkdir("shopyo") pycache_path2 = shopyo_path.mkdir("__pycache__") pyc2 = pycache_path2.join("file.pyc") pyc2.write("content") pycache_path3 = shopyo_path.mkdir("module").mkdir("__pycache__") pyc3 = pycache_path3.join("file.pyc") pyc3.write("content") os.chdir(tmpdir) clean(flask_app) captured = capfd.readouterr() expected_out = ( "[x] all tables dropped\n" "[x] __pycache__ successfully deleted\n" ) expected_err_shopyo_db = ( f"[ ] unable to delete {os.path.join(tmpdir, 'shopyo.db')}" ) expected_err_migrations = ( f"[ ] unable to delete {os.path.join(tmpdir, 'migrations')}" ) assert os.path.exists(pycache_path1) is False assert os.path.exists(pycache_path2) is False assert os.path.exists(pycache_path3) is False assert expected_out in captured.out assert expected_err_shopyo_db in captured.err assert expected_err_migrations in captured.err