diff --git a/.travis.yml b/.travis.yml index e2b718ae..97296634 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,17 +48,17 @@ matrix: - name: "Python 3.7 Tests" python: 3.7 env: TEST=bench - script: python -m unittest -v bench.tests.test_init + script: python -m unittest -v bench.tests.test_utils && python -m unittest -v bench.tests.test_init - name: "Python 3.8 Tests" python: 3.8 env: TEST=bench - script: python -m unittest -v bench.tests.test_init + script: python -m unittest -v bench.tests.test_utils && python -m unittest -v bench.tests.test_init - name: "Python 3.9 Tests" python: 3.9 env: TEST=bench - script: python -m unittest -v bench.tests.test_init + script: python -m unittest -v bench.tests.test_utils && python -m unittest -v bench.tests.test_init - name: "Python 3.7 Easy Install" python: 3.7 diff --git a/bench/tests/test_utils.py b/bench/tests/test_utils.py new file mode 100644 index 00000000..63147df2 --- /dev/null +++ b/bench/tests/test_utils.py @@ -0,0 +1,86 @@ +import os +import shutil +import unittest + +from bench.app import App +from bench.bench import Bench +from bench.utils import exec_cmd, is_valid_frappe_branch +from bench.utils.app import get_required_deps_url + + +class TestUtils(unittest.TestCase): + def test_app_utils(self): + git_url = "https://github.com/frappe/frappe" + branch = "develop" + app = App(name=git_url, branch=branch, bench=Bench(".")) + self.assertTrue( + all( + [ + app.name == git_url, + app.branch == branch, + app.tag == branch, + app.is_url == True, + app.on_disk == False, + app.org == "frappe", + app.url == git_url, + ] + ) + ) + + def test_get_required_deps_url(self): + self.assertEqual( + get_required_deps_url(git_url="https://github.com/frappe/frappe.git", branch=None, repo_name="frappe"), + "https://raw.github.com/frappe/frappe/develop/frappe/hooks.py", + ) + self.assertEqual( + get_required_deps_url( + git_url="https://github.com/frappe/frappe.git", branch="version-13", repo_name="frappe" + ), + "https://raw.github.com/frappe/frappe/version-13/frappe/hooks.py", + ) + + def test_is_valid_frappe_branch(self): + self.assertTrue(is_valid_frappe_branch("https://github.com/frappe/frappe", frappe_branch="")) + self.assertTrue(is_valid_frappe_branch("https://github.com/frappe/frappe", frappe_branch="develop")) + self.assertTrue(is_valid_frappe_branch("https://github.com/frappe/erpnext", frappe_branch="version-13")) + self.assertFalse(is_valid_frappe_branch("https://github.com/frappe/erpnext", frappe_branch="version-1")) + + def test_app_states(self): + bench_dir = "./sandbox" + sites_dir = os.path.join(bench_dir, "sites") + + if not os.path.exists(sites_dir): + os.makedirs(sites_dir) + + fake_bench = Bench(bench_dir) + + fake_bench.apps.set_states() + + self.assertTrue(hasattr(fake_bench.apps, "states")) + self.assertTrue(os.path.exists(os.path.join(sites_dir, "apps_states.json"))) + + fake_bench.apps.states = {"frappe": {"resolution": None, "version": "14.0.0-dev"}} + fake_bench.apps.update_apps_states() + + self.assertEqual(fake_bench.apps.states, {}) + + frappe_path = os.path.join(bench_dir, "apps", "frappe", "frappe") + + os.makedirs(frappe_path) + + with open(os.path.join(frappe_path, "__init__.py"), "w+") as f: + f.write("__version__ = '11.0'") + + fake_bench.apps.update_apps_states("frappe") + + self.assertIn("frappe", fake_bench.apps.states) + self.assertIn("version", fake_bench.apps.states["frappe"]) + self.assertEqual("11.0", fake_bench.apps.states["frappe"]["version"]) + + shutil.rmtree(bench_dir) + + def test_get_dependencies(self): + fake_app = App("frappe/healthcare@develop") + self.assertIn("erpnext", fake_app._get_dependencies()) + fake_app = App("frappe/not_healthcare@not-a-branch") + self.assertTrue(len(fake_app._get_dependencies()) == 0) diff --git a/bench/utils/app.py b/bench/utils/app.py index 6d5f0f32..a04d60ac 100644 --- a/bench/utils/app.py +++ b/bench/utils/app.py @@ -107,7 +107,9 @@ def switch_to_develop(apps=None, bench_path=".", upgrade=True): def get_version_from_string(contents, field="__version__"): - match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])(?sm)" % field, contents) + match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])" % field, contents, flags=(re.S | re.M)) + if not match: + raise Exception("No match was found") return match.group(2) @@ -174,6 +176,15 @@ def get_required_deps(org, name, branch, deps="hooks.py"): return base64.decodebytes(res["content"].encode()).decode() +def get_required_deps_url(git_url, repo_name, branch, deps="hooks.py"): + git_url = ( + git_url.replace(".git", "").replace("github.com", "raw.github.com") + ) + branch = branch if branch else "develop" + git_url += f"/{branch}/{repo_name}/{deps}" + return git_url + + def get_remote(app, bench_path="."): repo_dir = get_repo_dir(app, bench_path=bench_path) contents = subprocess.check_output( @@ -215,6 +226,12 @@ def check_existing_dir(bench_path, repo_name): dir_already_exists = os.path.isdir(cloned_path) return dir_already_exists, cloned_path +def check_existing_dir(bench_path, repo_name): + cloned_path = os.path.join(bench_path, "apps", repo_name) + dir_already_exists = os.path.isdir(cloned_path) + return dir_already_exists, cloned_path + + def get_current_version(app, bench_path="."): current_version = None repo_dir = get_repo_dir(app, bench_path=bench_path)