diff --git a/src/west/app/project.py b/src/west/app/project.py index c9ea044d..7da55808 100644 --- a/src/west/app/project.py +++ b/src/west/app/project.py @@ -817,6 +817,10 @@ def init_state(self, args): self.narrow = args.narrow or config.getboolean('update', 'narrow', fallback=False) + self.path_cache = args.path_cache or config.get('update', 'path-cache', + fallback=None) + self.name_cache = args.name_cache or config.get('update', 'name-cache', + fallback=None) self.group_filter: List[str] = [] @@ -1151,26 +1155,29 @@ def init_project(self, project): def project_cache(self, project): # Find the absolute path to a pre-existing local clone of a project # and return it. If the search fails, return None. - name_cache, path_cache = self.args.name_cache, self.args.path_cache - if name_cache is not None: - maybe = Path(name_cache) / project.name + if self.name_cache is not None: + maybe = Path(self.name_cache) / project.name if maybe.is_dir(): - log.dbg(f'found {project.name} in --name-cache {name_cache}', - level=log.VERBOSE_VERY) + log.dbg( + f'found {project.name} in --name-cache {self.name_cache}', + level=log.VERBOSE_VERY) return os.fspath(maybe) else: - log.dbg(f'{project.name} not in --name-cache {name_cache}', - level=log.VERBOSE_VERY) - elif self.args.path_cache is not None: - maybe = Path(self.args.path_cache) / project.path + log.dbg( + f'{project.name} not in --name-cache {self.name_cache}', + level=log.VERBOSE_VERY) + elif self.path_cache is not None: + maybe = Path(self.path_cache) / project.path if maybe.is_dir(): - log.dbg(f'found {project.path} in --path-cache {path_cache}', - level=log.VERBOSE_VERY) + log.dbg( + f'found {project.path} in --path-cache {self.path_cache}', + level=log.VERBOSE_VERY) return os.fspath(maybe) else: - log.dbg(f'{project.path} not in --path-cache {path_cache}', - level=log.VERBOSE_VERY) + log.dbg( + f'{project.path} not in --path-cache {self.path_cache}', + level=log.VERBOSE_VERY) return None diff --git a/tests/test_project.py b/tests/test_project.py index 3160ac68..4a40a2ac 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -969,23 +969,34 @@ def test_update_name_cache(tmpdir): # network if it doesn't have to. name_cache_dir = tmpdir / 'name_cache' - create_repo(name_cache_dir / 'foo') create_repo(name_cache_dir / 'bar') - foo_head = rev_parse(name_cache_dir / 'foo', 'HEAD') bar_head = rev_parse(name_cache_dir / 'bar', 'HEAD') workspace = tmpdir / 'workspace' setup_cache_workspace(workspace, foo_head, bar_head) - - cmd(f'update --name-cache {name_cache_dir}', cwd=workspace) - - assert (workspace / 'subdir' / 'foo').check(dir=1) - assert (workspace / 'bar').check(dir=1) - - assert rev_parse(workspace / 'subdir' / 'foo', 'HEAD') == foo_head - assert rev_parse(workspace / 'bar', 'HEAD') == bar_head + workspace.chdir() + foo = workspace / 'subdir' / 'foo' + bar = workspace / 'bar' + + # Test the command line option. + cmd(f'update --name-cache {name_cache_dir}') + assert foo.check(dir=1) + assert bar.check(dir=1) + assert rev_parse(foo, 'HEAD') == foo_head + assert rev_parse(bar, 'HEAD') == bar_head + + # Move the repositories out of the way and test the configuration option. + # (We can't use shutil.rmtree here because Windows.) + shutil.move(os.fspath(foo), os.fspath(tmpdir)) + shutil.move(os.fspath(bar), os.fspath(tmpdir)) + cmd(f'config update.name-cache {name_cache_dir}') + cmd('update') + assert foo.check(dir=1) + assert bar.check(dir=1) + assert rev_parse(foo, 'HEAD') == foo_head + assert rev_parse(bar, 'HEAD') == bar_head def test_update_path_cache(tmpdir): @@ -993,23 +1004,34 @@ def test_update_path_cache(tmpdir): # network if it doesn't have to. path_cache_dir = tmpdir / 'path_cache_dir' - create_repo(path_cache_dir / 'subdir' / 'foo') create_repo(path_cache_dir / 'bar') - foo_head = rev_parse(path_cache_dir / 'subdir' / 'foo', 'HEAD') bar_head = rev_parse(path_cache_dir / 'bar', 'HEAD') workspace = tmpdir / 'workspace' setup_cache_workspace(workspace, foo_head, bar_head) - - cmd(f'update --path-cache {path_cache_dir}', cwd=workspace) - - assert (workspace / 'subdir' / 'foo').check(dir=1) - assert (workspace / 'bar').check(dir=1) - - assert rev_parse(workspace / 'subdir' / 'foo', 'HEAD') == foo_head - assert rev_parse(workspace / 'bar', 'HEAD') == bar_head + workspace.chdir() + foo = workspace / 'subdir' / 'foo' + bar = workspace / 'bar' + + # Test the command line option. + cmd(f'update --path-cache {path_cache_dir}') + assert foo.check(dir=1) + assert bar.check(dir=1) + assert rev_parse(foo, 'HEAD') == foo_head + assert rev_parse(bar, 'HEAD') == bar_head + + # Move the repositories out of the way and test the configuration option. + # (We can't use shutil.rmtree here because Windows.) + shutil.move(os.fspath(foo), os.fspath(tmpdir)) + shutil.move(os.fspath(bar), os.fspath(tmpdir)) + cmd(f'config update.path-cache {path_cache_dir}') + cmd('update') + assert foo.check(dir=1) + assert bar.check(dir=1) + assert rev_parse(foo, 'HEAD') == foo_head + assert rev_parse(bar, 'HEAD') == bar_head def setup_narrow(tmpdir):