def test__set_loaders_to_None_then_raises_RuntimeError_if_not_in_workspace( self): workspace._set_path(None) props._set_loaders(None) with self.assertRaises(RuntimeError): props.get(util.base36_str())
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 __exit__(self, exc_type, exc_val, exc_tb): workspace._set_path(self.existing) if exc_val is not None: raise exc_val else: return True
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 test_destination_root_default_raises_AssertionError_if_not_in_a_workspace(self): non_subclassed_generator = Generator() empty_dir = tempfile.mkdtemp() with TemporaryEnv(): workspace._set_path(None) os.chdir(empty_dir) with self.assertRaises(AssertionError): non_subclassed_generator.destination_root()
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_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 bootstrap_globals(prop_overrides): """ Bootstrap global variables. Used by LoR CLI to bootstrap CLI overrides (variable vals etc.) :param prop_overrides: :return: :raises RuntimeError: If not in a workspace :raises FileNotFoundError: If workspace properties.yml file is missing """ workspace_path = workspace.get_path() if workspace_path is None: raise RuntimeError( "Not currently in a workspace (or cannot locate one)") workspace._set_path(workspace_path) path._set_overlay_paths([]) # Not using overlay paths yet prop_file_path = os.path.join(workspace.get_path(), lor._constants.WORKSPACE_PROPS) if not os.path.exists(prop_file_path): raise FileNotFoundError( "{prop_file_path}: No such file: a properties file is *required* in the workspace when running LoR" ) loaders = [props.DictPropertyLoader("cli-overrides", prop_overrides) ] + props.get_loaders() props._set_loaders(loaders) # This allows workspaces to be loaded dynamically at runtime by LoR and Luigi # (the Luigi docs get clients to set PYTHONPATH explicitly) sys.path.insert(0, workspace.get_path())
def __enter__(self): self.existing = workspace.get_path() workspace._set_path(self.ws_path) return self.ws_path
def test_join_raises_ValueError_if_no_overlays_and_not_in_workspace(self): path._set_overlay_paths([]) workspace._set_path(None) with self.assertRaises(ValueError): path.join(util.base36_str())
def test__set_path_to_None_works_ok(self): workspace._set_path(None)
def test_get_path_returns_None_if_manual_ws_set_is_None_and_cwd_is_not_a_workspace( self): some_dir = tempfile.mkdtemp() workspace._set_path(None) self.assertIsNone(workspace.get_path(some_dir))
def test_resolve_raises_ValueError_if_overlays_empty_and_not_in_workspace(self): path._set_overlay_paths([]) workspace._set_path(None) with self.assertRaises(ValueError): path.resolve(util.base36_str())
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)
def test__set_path_raises_FileNotFoundError_if_arg_does_not_exist(self): with self.assertRaises(FileNotFoundError): workspace._set_path(util.base36_str())
def test__set_path_raises_NotADirectoryError_if_arg_is_file(self): _, file_path = tempfile.mkstemp() with self.assertRaises(NotADirectoryError): workspace._set_path(file_path)
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_resolve_all_list_is_empty_if_no_overlays_or_workspace(self): path._set_overlay_paths([]) workspace._set_path(None) self.assertEqual([], path.resolve_all())
def test__set_path_raises_ValueError_if_arg_is_no_a_workspace(self): some_dir = tempfile.mkdtemp() with self.assertRaises(ValueError): workspace._set_path(some_dir)