diff --git a/dev/dev-test.nu b/dev/dev-test.nu index 7fb62ab..b6d9bfc 100755 --- a/dev/dev-test.nu +++ b/dev/dev-test.nu @@ -26,7 +26,9 @@ const RUNNER = "dev-test" const COMP_FN = "_dev_test" # Completers cannot update the caller's environment, so the filter list is a file. -const FILTER_CACHE = "target/skyg-test-filters.cache" +# Repo-relative: `repo-root` turns it absolute, so a completion started in a +# subdirectory writes the same file under the same key. +const FILTER_CACHE_REL = "target/skyg-test-filters.cache" # One entry point for the repository's checks. # @@ -189,15 +191,30 @@ def "nu-complete dev-test filters" [] { def filter-cache-path [] { let override = ($env.SKYG_TEST_FILTER_CACHE? | default "") - if ($override | is-empty) { $FILTER_CACHE } else { $override } + if ($override | is-empty) { repo-root | path join $FILTER_CACHE_REL } else { $override } +} + +# The globs and the cache are anchored here, so the key does not depend on the +# completion's working directory (from `src/` the relative globs failed, which +# made the key "unknown" and the entry never stale). +def repo-root [] { + $SCRIPT_PATH | path dirname | path dirname } def source-mtime-key [] { - let rows = (ls src/**/*.rs tests/**/*.rs Cargo.toml Cargo.lock) + let root = (repo-root | path expand | into string) + # `ls` only expands globs written literally in the source, so the anchored + # patterns go through `glob`. + let rows = ( + (glob ($root + "/src/**/*.rs")) + ++ (glob ($root + "/tests/**/*.rs")) + ++ (glob ($root + "/Cargo.toml")) + ++ (glob ($root + "/Cargo.lock")) + ) if ($rows | is-empty) { return "none" } - $rows | get modified | math max | format date "%s.%f" + $rows | each {|p| ls $p | get 0.modified } | math max | format date "%s.%f" } # Null means a miss. An empty list is a hit that listed no tests. @@ -218,6 +235,11 @@ def write-cached-lines [path: string, key: string, names: list] { } def cached-lines [path: string, key: string, produce: closure] { + # A key that could not be computed ("unknown") or found no sources ("none") + # never changes, so caching under it would serve stale names forever. + if $key in ["unknown" "none"] { + return (do $produce) + } let hit = (read-cached-lines $path $key) if $hit != null { return $hit @@ -673,8 +695,22 @@ def check-dispatch [] { def check-filter-cache [] { mut problems = [] - if not ($FILTER_CACHE | str starts-with "target/") { - $problems = ($problems | append "filter cache must live under target/") + let root = (repo-root) + let rel = (filter-cache-path | path relative-to $root) + if $rel != $FILTER_CACHE_REL { + $problems = ($problems | append $"filter cache is '($rel)', expected '($FILTER_CACHE_REL)' under the repo root") + } + # A completion started in a subdirectory must key and store the same way. + let root_key = (source-mtime-key) + let from_sub = (do { + cd ($root | path join "src") + {key: (source-mtime-key), cache: (filter-cache-path)} + }) + if $from_sub.key != $root_key or $from_sub.key in ["unknown" "none"] { + $problems = ($problems | append $"source mtime key from src/ is '($from_sub.key)', from the repo root '($root_key)'") + } + if $from_sub.cache != ($from_sub.cache | path expand) { + $problems = ($problems | append $"filter cache path from src/ is relative: '($from_sub.cache)'") } let dir = ($nu.temp-dir | path join $"filter-cache-($nu.pid)") rm -rf $dir @@ -709,6 +745,15 @@ def check-filter-cache [] { if $ran.n2 != 2 { $problems = ($problems | append $"stale filter cache listed ($ran.n2) times after a stale key") } + # An "unknown" key cannot go stale, so it must not be stored. + let unknown_cache = ($dir | path join "unknown-cache") + let produced = (cached-lines $unknown_cache "unknown" {|| ["x"] }) + if ($unknown_cache | path exists) { + $problems = ($problems | append "an unknown cache key was written; that entry would never go stale") + } + if $produced != ["x"] { + $problems = ($problems | append $"an unknown cache key returned ($produced | to nuon), expected a fresh listing") + } rm -rf $dir $problems }