def test_DataPath_different_working_dir(): """Test that DataPath is not affected by current working dir.""" p = bazelutil.DataPath('phd/lib/labm8/data/test/hello_world') with fs.chdir('/tmp'): assert bazelutil.DataPath('phd/lib/labm8/data/test/hello_world') == p with tempfile.TemporaryDirectory() as d: with fs.chdir(d): assert bazelutil.DataPath( 'phd/lib/labm8/data/test/hello_world') == p
def test_IsBazelSandbox_different_working_directories(): """Test IsBazelSandboxed() returns the same result from different dirs.""" sandbox = bazelutil.IsBazelSandbox() # We can't test the expected value of this since we don't know it. assert sandbox or True with tempfile.TemporaryDirectory() as d: with fs.chdir(d): assert bazelutil.IsBazelSandbox() == sandbox with fs.chdir('/tmp'): assert bazelutil.IsBazelSandbox() == sandbox
def test_chdir_cwd(): """Test that chdir() correctly changes the working directory.""" with tempfile.TemporaryDirectory() as d: with fs.chdir(d): # Bazel sandboxing only changes to directories within the sandbox, so # there may be an unwanted prefix like /private that we can ignore. assert os.getcwd().endswith(d)
def GetAllFilesRelativePaths( root_dir: pathlib.Path, follow_symlinks: bool = False) -> typing.List[str]: """Get relative paths to all files in the root directory. Follows symlinks. Args: root_dir: The directory to find files in. follow_symlinks: If true, follow symlinks. Returns: A list of paths relative to the root directory. Raises: EmptyCorpusException: If the content files directory is empty. """ with fs.chdir(root_dir): cmd = ['find'] if follow_symlinks: cmd.append('-L') cmd += ['.', '-type', 'f'] find_output = subprocess.check_output(cmd).decode('utf-8').strip() if find_output: # Strip the leading './' from paths. return [x[2:] for x in find_output.split('\n')] else: return []
def test_chdir_file_argument(): """Test that NotADirectoryError is raised if requested path is a file.""" with tempfile.NamedTemporaryFile(prefix='labm8_') as f: with pytest.raises(NotADirectoryError) as e_info: with fs.chdir(f.name): pass # Bazel sandboxing only changes to directories within the sandbox, so there # may be an unwanted prefix like /private that we can ignore. assert f"Not a directory: '" in str(e_info) assert f.name in str(e_info)
def GetImportRelpaths(self, contentfile_root: pathlib.Path) -> typing.List[str]: """Get relative paths to all files in the content files directory. Args: contentfile_root: The root of the content files directory. Returns: A list of paths relative to the content files root. Raises: EmptyCorpusException: If the content files directory is empty. """ with fs.chdir(contentfile_root): find_output = subprocess.check_output(['find', '.', '-type', 'f' ]).decode('utf-8').strip() if not find_output: raise errors.EmptyCorpusException( f"Empty content files directory: '{contentfile_root}'") return find_output.split('\n')
def test_chdir_not_a_directory(): """Test that FileNotFoundError is raised if requested path does not exist.""" with pytest.raises(FileNotFoundError) as e_info: with fs.chdir('/not/a/real/path'): pass assert "No such file or directory: '/not/a/real/path'" in str(e_info)
def test_chdir_yield_value(): """Test that chdir() yields the requested directory as a pathlib.Path.""" with tempfile.TemporaryDirectory() as d: with fs.chdir(d) as d2: assert pathlib.Path(d) == d2