def test_resolve_all_list_contains_elements_that_do_exist(self): file_name = util.base36_str() overlay_with_result = tempfile.mkdtemp() self.__create_file(os.path.join(overlay_with_result, file_name)) empty_overlay = tempfile.mkdtemp() another_overlay_with_result = tempfile.mkdtemp() self.__create_file(os.path.join(another_overlay_with_result, file_name)) path._set_overlay_paths([ overlay_with_result, empty_overlay, another_overlay_with_result ]) ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) self.__create_file(os.path.join(ws_path, file_name)) workspace._set_path(ws_path) expected_result = [ os.path.join(overlay_with_result, file_name), os.path.join(another_overlay_with_result, file_name), os.path.join(ws_path, file_name), ] actual_result = path.resolve_all(file_name) self.assertEqual(expected_result, actual_result)
def test_get_package_name_returns_a_string_for_new_workspace(self): cwd = os.path.join(tempfile.mkdtemp(), "wd") workspace_generator.create(cwd) ret = workspace.get_package_name(cwd) self.assertIsInstance(ret, str)
def test_try_locate_returns_cwd_if_cwd_is_a_workspace(self): cwd = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(cwd) ws_path = workspace.try_locate(cwd) self.assertEqual(os.path.realpath(cwd), os.path.realpath(ws_path))
def test_run_install_script_runs_bin_install(self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) installer_path = os.path.join(ws_path, lor._constants.WORKSPACE_INSTALL_BINSTUB) os.remove(installer_path) workspace_dir = ws_path install_path = os.path.join(workspace_dir, lor._constants.WORKSPACE_INSTALL_BINSTUB) str_written_by_script = util.base36_str(20) _, file_containing_str = tempfile.mkstemp() install_script = """#!/bin/bash echo {str} > {out} """.format(str=str_written_by_script, out=file_containing_str) with open(install_path, "w") as f: f.write(install_script) st = os.stat(str(install_path)) os.chmod(install_path, st.st_mode | stat.S_IEXEC) workspace.run_install_script(workspace_dir) self.assertTrue(os.path.exists(file_containing_str)) with open(file_containing_str, "r") as f: self.assertEqual(str_written_by_script, f.readlines()[0].strip())
def test_try_locate_returns_env_cwd_if_env_cwd_is_a_workspace(self): cwd = os.path.join(tempfile.mkdtemp(), "wd") workspace_generator.create(cwd) with TemporaryEnv(): os.chdir(cwd) ws_path = workspace.try_locate() self.assertEqual(os.path.realpath(cwd), os.path.realpath(ws_path))
def test_get_path_returns_manually_set_path(self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) returned_path = workspace.get_path() self.assertEqual(ws_path, returned_path)
def run(self, argv): parser = argparse.ArgumentParser(description=self.description()) parser.add_argument("workspace_path", type=str, help="Path to newly-created workspace") parsed_args = parser.parse_args(argv) workspace_generator.create(parsed_args.workspace_path)
def test_try_locate_returns_cwd_if_workspace_is_a_parent_of_cwd(self): cwd = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(cwd) some_subdir = os.path.join(cwd, 'some', 'subdir', 'in', 'ws') os.makedirs(some_subdir) ws_path = workspace.try_locate(some_subdir) self.assertEqual(os.path.realpath(cwd), os.path.realpath(ws_path))
def test_run_install_script_raises_FileNotFoundError_if_workspace_doesnt_contain_installer( self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) installer_path = os.path.join(ws_path, lor._constants.WORKSPACE_INSTALL_BINSTUB) os.remove(installer_path) with self.assertRaises(FileNotFoundError): workspace.run_install_script(ws_path)
def test_get_package_name_raises_Exception_if_properties_file_missing_from_workspace( self): cwd = os.path.join(tempfile.mkdtemp(), "wd") workspace_generator.create(cwd) props_file = os.path.join(cwd, lor._constants.WORKSPACE_PROPS) os.remove(props_file) with self.assertRaises(Exception): workspace.get_package_name(cwd)
def test_try_locate_returns_LOR_HOME_if_it_is_set_in_env(self): cwd = os.path.join(tempfile.mkdtemp(), "wd") workspace_generator.create(cwd) with TemporaryEnv(): os.environ[lor._constants.WORKSPACE_ENV_VARNAME] = cwd ws_path = workspace.try_locate() self.assertEqual(os.path.realpath(cwd), os.path.realpath(ws_path))
def test_get_path_returns_cwd_if_cwd_is_workspace_and_no_manual_override_set( self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(None) returned_path = workspace.get_path(ws_path) self.assertEqual(ws_path, returned_path)
def test_join_returns_paths_joined_onto_workspace_if_no_overlay_paths_available(self): path._set_overlay_paths([]) ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) fragment = util.base36_str() expected_result = os.path.join(ws_path, fragment) actual_result = path.join(fragment) self.assertEqual(expected_result, actual_result)
def test_resolve_raises_FileNotFoundError_if_file_doesnt_exist_in_any_location(self): overlays = [ tempfile.mkdtemp(), tempfile.mkdtemp(), tempfile.mkdtemp() ] path._set_overlay_paths(overlays) ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) with self.assertRaises(FileNotFoundError): path.resolve(util.base36_str())
def test_resolve_returns_result_from_workspace_if_workspace_has_file(self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) file_name = util.base36_str() self.__create_file(os.path.join(ws_path, file_name)) some_overlay = tempfile.mkdtemp() path._set_overlay_paths([some_overlay]) returned_path = path.resolve(file_name) self.assertEqual(os.path.join(ws_path, file_name), returned_path)
def test_get_package_name_uses_WORKSPACE_NAME_variable(self): cwd = os.path.join(tempfile.mkdtemp(), "wd") workspace_generator.create(cwd) props_file = os.path.join(cwd, lor._constants.WORKSPACE_PROPS) ws_name = util.base36_str() content = "WORKSPACE_NAME: " + ws_name os.remove(props_file) util.write_str_to_file(props_file, content) ret = workspace.get_package_name(cwd) self.assertEqual(ret, ws_name)
def test_join_returns_paths_joined_onto_first_overlay_if_overlays_assigned(self): first_overlay = tempfile.mkdtemp() overlays = [ first_overlay, tempfile.mkdtemp(), ] path._set_overlay_paths(overlays) ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) fragment = util.base36_str() expected_result = os.path.join(first_overlay, fragment) actual_result = path.join(fragment) self.assertEqual(expected_result, actual_result)
def test_join_all_returns_list_of_overlays_then_workspace(self): overlay1 = tempfile.mkdtemp() overlay2 = tempfile.mkdtemp() overlay3 = tempfile.mkdtemp() overlays = [overlay1, overlay2, overlay3] path._set_overlay_paths(overlays) ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path) frag = util.base36_str() expected_ret = [ os.path.join(p, frag) for p in [overlay1, overlay2, overlay3, ws_path] ] actual_ret = path.join_all(frag) self.assertEqual(expected_ret, actual_ret)
def __enter__(self): self.existing = workspace.get_path() ws_path = os.path.join(tempfile.mkdtemp(), "ws") ws = workspace_generator.create(ws_path) workspace._set_path(ws) return ws
def test__set_path_runs_ok_with_valid_workspace(self): ws_path = os.path.join(tempfile.mkdtemp(), "ws") workspace_generator.create(ws_path) workspace._set_path(ws_path)