def test_is_dir_link(mocker, tmp_path): """Should test if a path is a symlink or directory junction""" link_path = tmp_path / "link" targ_path = tmp_path / "target" mock_sys = mocker.patch.object(utils.helpers, "sys") mock_platform = type(mock_sys).platform = mocker.PropertyMock() mock_path = mocker.patch.object(utils.helpers, "Path").return_value mock_path.is_symlink.side_effect = [True, False, False, False] # Test Symlink (POSIX) mock_platform.return_value = "linux" assert utils.is_dir_link(link_path) assert not utils.is_dir_link(link_path) # Test Directory Junction (Windows) mock_platform.return_value = "win32" # From what I can tell, while Path.is_symlink always returns false for DJs. # However, on a DJ, Path.absolute will return the absolute path to the DJ, # while Path.resolve will return the absolute path to the source directory. # With this in mind, this check SHOULD work. mock_path.resolve.return_value = targ_path mock_path.absolute.return_value = link_path assert utils.is_dir_link(link_path) mock_path.absolute.return_value = targ_path assert not utils.is_dir_link(link_path)
def resolve_link(cls, stub, link_path): """Resolve or Create Stub Symlink Args: stub (Stub): stub to resolve link_path (str): path to link Returns: Stub: Stub from symlink """ fware = stub.firmware if utils.is_dir_link(link_path): return cls(link_path, firmware=fware) utils.create_dir_link(link_path, stub.path) return cls(link_path, firmware=fware)