Module: Mutineer::Pairing
- Defined in:
- lib/mutineer/pairing.rb
Overview
Source -> test pairing by path convention (#11). Pure stdlib path logic: no Rails, no class loading, no process. Six jobs:
* expand_sources — a directory argument becomes its sorted **/*.rb files;
a file inside the root becomes its root-relative path.
* expand_test: one --test value (#380). A directory becomes the test
files under it. A glob becomes the files it matches.
An existing file stays as typed.
* resolve_test_map — expand each `tests:` map entry's source and test
patterns (#382), clean and dedupe the file lists, and
record every pattern that matched no file.
* mapped_tests_for — the mapped tests for one source from a resolved
map, concatenated in entry order (#382).
* infer_tests — a source's test files by convention (app/ and lib/
sources map to test/.../_test.rb, test/.../<name>_*_test.rb,
test/.../test_*.rb or spec/.../_spec.rb), preserving
namespaced subdirectories. The first EXISTING exact
candidate stays first. Split Minitest files join a
Minitest match. A matched spec is left as that one file.
A concern at app/<dir>/concerns/<rest>.rb with no test
there also tries exact and split names at <dir>/<rest>.
A longer source keeps its file. A model keeps a split
file it already pairs with (#408).
* test_given_as_source? — whether a positional argument is a test file
by these same names, given as a source by mistake
after `--test` (#293).
Independently unit-testable: every method is pure in/out over the filesystem, so the pairing contract is exercised with plain fixtures, no Rails, no fork.
Constant Summary collapse
- TEST_GLOB_MARK =
Characters that make a --test value a glob when no path has that name. An existing file is never read as a glob, so a name may contain these.
/[*?\[{]/- GLOB_META =
Characters Dir.glob reads as a pattern. A real directory name may contain them. They are escaped so the name stays a literal prefix.
/([*?\[\]{}\\])/- TEST_NAME =
Basenames that pair as tests in candidates:
*_test.rb,*_spec.rband Minitest'stest_*.rb, but not itstest_helper.rbsupport file. /\A(?!test_helper\.rb\z)(?:.+_(?:test|spec)|test_.+)\.rb\z/
Class Method Summary collapse
-
.candidates(base, lib, prefer) ⇒ Array<String>
Ordered candidate test paths: test.rb, then Minitest's test*.rb, then _spec.rb.
-
.claimed_by_longer_concern?(root, ns_base, entry) ⇒ Boolean
True when a longer concern pairs with this split name.
-
.claimed_by_longer_source?(root, base, entry, scanned_dir = nil) ⇒ Boolean
True when a longer source file on disk owns this split test.
-
.classify_test_value(value, project_root:) ⇒ Symbol
How one --test value is read.
-
.directory_test_files(abs, root, roots) ⇒ Array<String>
Test files under a directory.
-
.existing_relative(abs, roots) ⇒ String?
An existing file's root-relative path, or nil outside the root.
-
.expand_map_patterns(patterns, number, key, unmatched) {|pattern| ... } ⇒ Array<String>
private
Expand each pattern for one map key.
-
.expand_map_source_pattern(pattern, root, roots) ⇒ Array<String>
private
One map source pattern: Dir.glob then Pairing.expand_sources, so a matched directory becomes its
.rbfiles and path spellings clean the same way. -
.expand_map_test_glob(pattern, root, roots) ⇒ Array<String>
private
Glob matches for a map
tests:pattern. -
.expand_map_test_pattern(pattern, root, roots) ⇒ Array<String>
private
One map test pattern.
-
.expand_sources(args, project_root:) ⇒ Array<String>
Expand each positional source: a directory -> its sorted **/*.rb files (relative to project_root); a file (or glob, or anything non-directory) -> its path relative to project_root, so
./lib/x.rband an absolute path pair likelib/x.rb(#104). -
.expand_test(value, project_root:) ⇒ Array<String>
One --test value, as paths.
-
.fs_children(dir) ⇒ Array<String>
Dir.childrenwith each name read as UTF-8, or binary when it is not (#346). -
.fs_glob(*args, **kwargs) ⇒ Array<String>
Dir.globwith each path read as UTF-8, or binary when it is not (#346). -
.glob_file(match, root, roots) ⇒ String?
A glob match that is a file, as a stored path.
-
.glob_literal(path) ⇒ String
A path used as a literal prefix in Dir.glob.
-
.glob_test_files(pattern, root, roots) ⇒ Array<String>
Files a glob matches from the project root.
-
.infer_test(source_rel, project_root:, prefer: "minitest") ⇒ String?
The first test path #infer_tests would run for a source, or nil.
-
.infer_tests(source_rel, project_root:, prefer: "minitest") ⇒ Array<String>
Every test file convention pairs with a source.
-
.inside_project?(dir_abs, root_abs) ⇒ Boolean
True when
dir_absis the project root or a directory inside it. -
.logical_path(source_rel) ⇒ Array(String, Boolean)
Strip the source root to a logical path (no ".rb") and flag lib/ sources.
-
.longer_owner_stems(name, entry) ⇒ Array<String>
Longer basenames that own
entry, one per prefix of its pieces. -
.longer_owns_entry?(entry, stem) ⇒ Boolean
True when
entryis the exact test, or a split test with a non-empty piece, forstem. -
.longer_source_exists?(root, dir, stem, scanned_dir = nil) ⇒ Boolean
True when a longer source file exists beside this logical path.
-
.mapped_tests_for(source, resolved) ⇒ Array<String>
Mapped test files for one source from a resolved map.
-
.mirror_dir(prefix, base) ⇒ String
Relative test directory for a logical source path.
-
.named_test_file(match, root, roots) ⇒ String?
A directory match whose basename is a test name, as a stored path.
-
.namespace_base(source_rel) ⇒ String?
Namespace mirror for a concern, or nil.
-
.namespace_concerns_dir(ns_base) ⇒ String?
app/<dir>/concerns/<rest-dir>for a namespace base<dir>/<rest>. -
.namespace_tests(ns, root, prefer) ⇒ Array<String>
Namespace exact and split tests for a concern whose
concerns/path has none. -
.resolve_test_map(test_map, project_root:) ⇒ Hash{Symbol => Array}
Expand every entry of a checked
test_mapinto cleaned file lists. -
.root_relative(abs, roots) ⇒ String?
absrelative to the first ofrootsit is under, or nil. -
.source_searches_dir?(source_rel, scanned_dir) ⇒ Boolean
True when pairing this source looks in
scanned_dir. -
.split_entry?(entry, name) ⇒ Boolean
True when
entryis<name>_<piece>_test.rband not the exact<name>_test.rbfile. -
.split_test_files(base, lib, root) ⇒ Array<String>
<basename>_*_test.rbfiles in each mirrored test directory (#87). -
.split_tests_in(root, dir_rel, name, base) ⇒ Array<String>
Split test files directly inside
dir_rel(not in subdirectories). -
.test_given_as_source?(path, project_root:, tests:) ⇒ Boolean
Whether the
runguard rejects a positional argument as a test file given as a source (#293). -
.tests_at(base, lib, root, prefer) ⇒ Array<String>
Exact and split tests for one logical base.
Instance Method Summary collapse
-
#candidates(base, lib, prefer) ⇒ Array<String>
private
Ordered candidate test paths: test.rb, then Minitest's test*.rb, then _spec.rb.
-
#claimed_by_longer_concern?(root, ns_base, entry) ⇒ Boolean
private
True when a longer concern pairs with this split name.
-
#claimed_by_longer_source?(root, base, entry, scanned_dir = nil) ⇒ Boolean
private
True when a longer source file on disk owns this split test.
-
#classify_test_value(value, project_root:) ⇒ Symbol
private
How one --test value is read.
-
#directory_test_files(abs, root, roots) ⇒ Array<String>
private
Test files under a directory.
-
#existing_relative(abs, roots) ⇒ String?
private
An existing file's root-relative path, or nil outside the root.
-
#expand_map_patterns(patterns, number, key, unmatched) {|pattern| ... } ⇒ Array<String>
private
private
Expand each pattern for one map key.
-
#expand_map_source_pattern(pattern, root, roots) ⇒ Array<String>
private
private
One map source pattern: Dir.glob then Pairing.expand_sources, so a matched directory becomes its
.rbfiles and path spellings clean the same way. -
#expand_map_test_glob(pattern, root, roots) ⇒ Array<String>
private
private
Glob matches for a map
tests:pattern. -
#expand_map_test_pattern(pattern, root, roots) ⇒ Array<String>
private
private
One map test pattern.
-
#expand_sources(args, project_root:) ⇒ Array<String>
private
Expand each positional source: a directory -> its sorted **/*.rb files (relative to project_root); a file (or glob, or anything non-directory) -> its path relative to project_root, so
./lib/x.rband an absolute path pair likelib/x.rb(#104). -
#expand_test(value, project_root:) ⇒ Array<String>
private
One --test value, as paths.
-
#fs_children(dir) ⇒ Array<String>
private
Dir.childrenwith each name read as UTF-8, or binary when it is not (#346). -
#fs_glob(*args, **kwargs) ⇒ Array<String>
private
Dir.globwith each path read as UTF-8, or binary when it is not (#346). -
#glob_file(match, root, roots) ⇒ String?
private
A glob match that is a file, as a stored path.
-
#glob_literal(path) ⇒ String
private
A path used as a literal prefix in Dir.glob.
-
#glob_test_files(pattern, root, roots) ⇒ Array<String>
private
Files a glob matches from the project root.
-
#infer_test(source_rel, project_root:, prefer: "minitest") ⇒ String?
private
The first test path #infer_tests would run for a source, or nil.
-
#infer_tests(source_rel, project_root:, prefer: "minitest") ⇒ Array<String>
private
Every test file convention pairs with a source.
-
#inside_project?(dir_abs, root_abs) ⇒ Boolean
private
True when
dir_absis the project root or a directory inside it. -
#logical_path(source_rel) ⇒ Array(String, Boolean)
private
Strip the source root to a logical path (no ".rb") and flag lib/ sources.
-
#longer_owner_stems(name, entry) ⇒ Array<String>
private
Longer basenames that own
entry, one per prefix of its pieces. -
#longer_owns_entry?(entry, stem) ⇒ Boolean
private
True when
entryis the exact test, or a split test with a non-empty piece, forstem. -
#longer_source_exists?(root, dir, stem, scanned_dir = nil) ⇒ Boolean
private
True when a longer source file exists beside this logical path.
-
#mapped_tests_for(source, resolved) ⇒ Array<String>
private
Mapped test files for one source from a resolved map.
-
#mirror_dir(prefix, base) ⇒ String
private
Relative test directory for a logical source path.
-
#named_test_file(match, root, roots) ⇒ String?
private
A directory match whose basename is a test name, as a stored path.
-
#namespace_base(source_rel) ⇒ String?
private
Namespace mirror for a concern, or nil.
-
#namespace_concerns_dir(ns_base) ⇒ String?
private
app/<dir>/concerns/<rest-dir>for a namespace base<dir>/<rest>. -
#namespace_tests(ns, root, prefer) ⇒ Array<String>
private
Namespace exact and split tests for a concern whose
concerns/path has none. -
#resolve_test_map(test_map, project_root:) ⇒ Hash{Symbol => Array}
private
Expand every entry of a checked
test_mapinto cleaned file lists. -
#root_relative(abs, roots) ⇒ String?
private
absrelative to the first ofrootsit is under, or nil. -
#source_searches_dir?(source_rel, scanned_dir) ⇒ Boolean
private
True when pairing this source looks in
scanned_dir. -
#split_entry?(entry, name) ⇒ Boolean
private
True when
entryis<name>_<piece>_test.rband not the exact<name>_test.rbfile. -
#split_test_files(base, lib, root) ⇒ Array<String>
private
<basename>_*_test.rbfiles in each mirrored test directory (#87). -
#split_tests_in(root, dir_rel, name, base) ⇒ Array<String>
private
Split test files directly inside
dir_rel(not in subdirectories). -
#test_given_as_source?(path, project_root:, tests:) ⇒ Boolean
private
Whether the
runguard rejects a positional argument as a test file given as a source (#293). -
#tests_at(base, lib, root, prefer) ⇒ Array<String>
private
Exact and split tests for one logical base.
Class Method Details
.candidates(base, lib, prefer) ⇒ Array<String>
Ordered candidate test paths: test.rb, then Minitest's test*.rb, then _spec.rb. lib/ sources also get the test/lib/... and spec/lib/... layouts.
500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/mutineer/pairing.rb', line 500 def candidates(base, lib, prefer) minitest = ["test/#{base}_test.rb"] minitest << "test/lib/#{base}_test.rb" if lib unless File.basename(base) == "helper" # test/test_helper.rb is Minitest's support file, not a test prefixed = base.sub(%r{[^/]+\z}) { |name| "test_#{name}" } minitest << "test/#{prefixed}.rb" minitest << "test/lib/#{prefixed}.rb" if lib end rspec = ["spec/#{base}_spec.rb"] rspec << "spec/lib/#{base}_spec.rb" if lib prefer == "rspec" ? rspec + minitest : minitest + rspec end |
.claimed_by_longer_concern?(root, ns_base, entry) ⇒ Boolean
True when a longer concern pairs with this split name.
The file sits under app/<dir>/concerns/, a directory
longer_source_exists? does not search for a namespace base.
The longer concern claims the name only when its concerns/ path has
no test, because only then does it look at the namespace path.
A model keeps a split file it already pairs with (#408).
688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/mutineer/pairing.rb', line 688 def claimed_by_longer_concern?(root, ns_base, entry) dir = namespace_concerns_dir(ns_base) return false unless dir longer_owner_stems(File.basename(ns_base), entry).any? do |longer| rel = File.join(dir, "#{longer}.rb") abs = File.(rel, root) # A source path with `..` must not make this look at a file outside the project. next false unless inside_project?(abs, root) && File.file?(abs) base, lib = logical_path(rel) tests_at(base, lib, root, "minitest").empty? end end |
.claimed_by_longer_source?(root, base, entry, scanned_dir = nil) ⇒ Boolean
True when a longer source file on disk owns this split test. user.rb
does not take user_session_test.rb when user_session.rb exists in the
same directory, under app/, or under lib/. An intermediate file
counts: bar_upsert.rb owns bar_upsert_guards_test.rb, so bar.rb
does not.
670 671 672 673 674 675 |
# File 'lib/mutineer/pairing.rb', line 670 def claimed_by_longer_source?(root, base, entry, scanned_dir = nil) dir = File.dirname(base) longer_owner_stems(File.basename(base), entry).any? do |longer| longer_source_exists?(root, dir, longer, scanned_dir) end end |
.classify_test_value(value, project_root:) ⇒ Symbol
How one --test value is read. Blank is not a directory: File.expand_path of "" is the project root, and that would select every test.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mutineer/pairing.rb', line 84 def classify_test_value(value, project_root:) return :blank if value.nil? || value.strip.empty? abs = File.(value, project_root) return :directory if File.directory?(abs) return :file if File.exist?(abs) return :glob if value.match?(TEST_GLOB_MARK) :plain end |
.directory_test_files(abs, root, roots) ⇒ Array<String>
Test files under a directory. The name rule is TEST_NAME. Dot entries stay out, because Dir.glob skips them. The result is sorted. The directory name is literal, even when it contains a pattern character.
198 199 200 201 202 203 204 205 |
# File 'lib/mutineer/pairing.rb', line 198 def directory_test_files(abs, root, roots) listed = if (rel = root_relative(abs, roots)) fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root) else fs_glob(File.join(glob_literal(abs), "**", "*.rb")) end listed.filter_map { |match| named_test_file(match, root, roots) }.sort_by(&:b) end |
.existing_relative(abs, roots) ⇒ String?
An existing file's root-relative path, or nil outside the root. Tries the
path as given, then with its directory resolved, so a path typed through
a symlinked parent (or macOS /var under a /private/var root) matches.
The file's own name is kept, so a symlinked file is not renamed.
363 364 365 366 |
# File 'lib/mutineer/pairing.rb', line 363 def existing_relative(abs, roots) root_relative(abs, roots) || root_relative(File.join(File.realpath(File.dirname(abs)), File.basename(abs)), roots) end |
.expand_map_patterns(patterns, number, key, unmatched) {|pattern| ... } ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Expand each pattern for one map key. An empty expansion records the
pattern under unmatched. Results are deduped and sorted.
262 263 264 265 266 267 268 |
# File 'lib/mutineer/pairing.rb', line 262 def (patterns, number, key, unmatched) patterns.flat_map do |pattern| files = yield(pattern) unmatched << { entry: number, key: key, pattern: pattern } if files.empty? files end.uniq.sort_by(&:b) end |
.expand_map_source_pattern(pattern, root, roots) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
One map source pattern: Dir.glob then expand_sources, so a matched
directory becomes its .rb files and path spellings clean the same way.
Matches outside the project root are dropped (brace patterns like
{..,app}/x.rb can escape a text-only .. check).
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/mutineer/pairing.rb', line 280 def (pattern, root, roots) # An existing file or directory is literal, even when its name holds a # glob character (same rule as {classify_test_value} for --test). abs = File.(pattern, root) listed = if File.exist?(abs) [pattern] else fs_glob(pattern, base: root) end return [] if listed.empty? (listed, project_root: root).filter_map do |path| path_abs = File.(path, root) next unless File.exist?(path_abs) existing_relative(path_abs, roots) end.uniq.sort_by(&:b) end |
.expand_map_test_glob(pattern, root, roots) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Glob matches for a map tests: pattern. A matched directory expands with
directory_test_files; a matched file is kept as-is (no name filter).
--test globs still drop directories; only the map expands them (#382).
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/mutineer/pairing.rb', line 337 def (pattern, root, roots) matches = if File.absolute_path?(pattern) fs_glob(pattern) else fs_glob(pattern, base: root) end matches.flat_map do |match| full = File.absolute_path?(match) ? match : File.(match, root) if File.directory?(full) directory_test_files(full, root, roots) elsif File.file?(full) [existing_relative(full, roots) || full] else [] end end end |
.expand_map_test_pattern(pattern, root, roots) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
One map test pattern. Directories (typed or matched by a glob) expand
with the TEST_NAME filter, matching a typed --test directory. Files
a glob matches stay unfiltered (including test_helper.rb), matching
--test globs. A missing plain path is not a match. Matches outside the
project root are dropped (#382 review).
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/mutineer/pairing.rb', line 310 def (pattern, root, roots) listed = case classify_test_value(pattern, project_root: root) when :directory directory_test_files(File.(pattern, root), root, roots) when :glob (pattern, root, roots) else # :file / :plain / :blank — same as {expand_test}, then root-filter. (pattern, project_root: root) end listed.filter_map do |path| abs = File.(path, root) next unless File.file?(abs) existing_relative(abs, roots) end.uniq.sort_by(&:b) end |
.expand_sources(args, project_root:) ⇒ Array<String>
Expand each positional source: a directory -> its sorted **/*.rb files
(relative to project_root); a file (or glob, or anything non-directory)
-> its path relative to project_root, so ./lib/x.rb and an absolute
path pair like lib/x.rb (#104). A missing file, or one outside the
root, stays as typed. Flattened, deduped, order-stable.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mutineer/pairing.rb', line 47 def (args, project_root:) root = FsPath.read(File.(project_root)) # The root as typed and as its real path (macOS `/var` vs `/private/var`), # resolved once for every file below. roots = [root, ProjectPath.root_real(root)].uniq Array(args).flat_map do |arg| arg = FsPath.read(arg) abs = File.(arg, root) if File.directory?(abs) # Escape metacharacters in the directory name so `component[1]` stays # literal (same rule as {directory_test_files}). listed = if (rel = root_relative(abs, roots)) fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root) else fs_glob(File.join(glob_literal(abs), "**", "*.rb")) end listed.sort_by(&:b).map do |f| full = File.absolute_path?(f) ? f : File.(f, root) existing_relative(full, roots) || full end else # `..` resolves as text, as FileSwap and the daemon already read it. [(existing_relative(abs, roots) if File.exist?(abs)) || arg] end end.map { |path| FsPath.read(path) }.uniq end |
.expand_test(value, project_root:) ⇒ Array<String>
One --test value, as paths. [] means a directory or glob matched nothing. A file stays as typed. A missing plain path stays as typed, so the CLI can still say "no such file". Paths from a directory or glob are sorted. Inside the project they are root-relative. Outside it they are absolute.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mutineer/pairing.rb', line 103 def (value, project_root:) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq case classify_test_value(value, project_root: project_root) when :directory directory_test_files(File.(value, root), root, roots) when :glob glob_test_files(value, root, roots) else [value] end end |
.fs_children(dir) ⇒ Array<String>
Dir.children with each name read as UTF-8, or binary when it is not (#346).
186 187 188 |
# File 'lib/mutineer/pairing.rb', line 186 def fs_children(dir) Dir.children(dir).map { |path| FsPath.read(path) } end |
.fs_glob(*args, **kwargs) ⇒ Array<String>
Dir.glob with each path read as UTF-8, or binary when it is not (#346).
178 179 180 |
# File 'lib/mutineer/pairing.rb', line 178 def fs_glob(*args, **kwargs) Dir.glob(*args, **kwargs).map { |path| FsPath.read(path) } end |
.glob_file(match, root, roots) ⇒ String?
A glob match that is a file, as a stored path. Directories are dropped.
244 245 246 247 248 249 |
# File 'lib/mutineer/pairing.rb', line 244 def glob_file(match, root, roots) full = File.absolute_path?(match) ? match : File.(match, root) return unless File.file?(full) existing_relative(full, roots) || full end |
.glob_literal(path) ⇒ String
A path used as a literal prefix in Dir.glob.
170 171 172 |
# File 'lib/mutineer/pairing.rb', line 170 def glob_literal(path) FsPath.read(path).gsub(GLOB_META) { "\\#{Regexp.last_match(1)}" } end |
.glob_test_files(pattern, root, roots) ⇒ Array<String>
Files a glob matches from the project root. Directories are ignored. There is no test-name filter. An absolute pattern ignores base. Dir.glob keeps a leading "./"; the path is then made root-relative.
215 216 217 218 219 220 221 222 |
# File 'lib/mutineer/pairing.rb', line 215 def glob_test_files(pattern, root, roots) listed = if File.absolute_path?(pattern) fs_glob(pattern) else fs_glob(pattern, base: root) end listed.filter_map { |match| glob_file(match, root, roots) }.sort_by(&:b) end |
.infer_test(source_rel, project_root:, prefer: "minitest") ⇒ String?
The first test path #infer_tests would run for a source, or nil.
prefer is the resolved framework ("minitest" | "rspec"): its candidates
are tried first, the other framework's as fallback, so a minitest default
still finds a spec and vice-versa.
398 399 400 |
# File 'lib/mutineer/pairing.rb', line 398 def infer_test(source_rel, project_root:, prefer: "minitest") infer_tests(source_rel, project_root: project_root, prefer: prefer).first end |
.infer_tests(source_rel, project_root:, prefer: "minitest") ⇒ Array<String>
Every test file convention pairs with a source. The first existing exact
candidate from #candidates stays first. Each <basename>_*_test.rb in
the mirrored test directory is added after it (#87). A spec that the exact
rules already found is left alone: split Minitest names do not join it
and do not replace it. When that list is empty, a concern tries the same
exact and split names at its namespace path (#408). The concerns/ path
still wins when it has any test. A longer source, including a longer
concern, keeps a split file. A model keeps a split file it already pairs
with: this fallback does not take one away.
416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/mutineer/pairing.rb', line 416 def infer_tests(source_rel, project_root:, prefer: "minitest") base, lib = logical_path(source_rel) root = File.(project_root) found = tests_at(base, lib, root, prefer) return found unless found.empty? ns = namespace_base(source_rel) return found unless ns namespace_tests(ns, root, prefer) end |
.inside_project?(dir_abs, root_abs) ⇒ Boolean
True when dir_abs is the project root or a directory inside it.
A source path such as ../../evil.rb must not list tests outside the project.
629 630 631 632 633 634 635 636 |
# File 'lib/mutineer/pairing.rb', line 629 def inside_project?(dir_abs, root_abs) dir_b = FsPath.read(dir_abs).b root_b = FsPath.read(root_abs).b return true if dir_b == root_b prefix = root_b == "/".b ? "/".b : root_b + "/".b FsPath.bytes_start_with?(dir_b, prefix) end |
.logical_path(source_rel) ⇒ Array(String, Boolean)
Strip the source root to a logical path (no ".rb") and flag lib/ sources. app/foo/bar.rb and lib/foo/bar.rb both -> "foo/bar"; anything else -> the path minus ".rb" (still attempted). Namespaced subdirs are preserved verbatim — structural, never constant resolution.
482 483 484 485 486 487 488 489 490 491 |
# File 'lib/mutineer/pairing.rb', line 482 def logical_path(source_rel) no_ext = source_rel.sub(/\.rb\z/, "") if no_ext.start_with?("app/") [no_ext.sub(%r{\Aapp/}, ""), false] elsif no_ext.start_with?("lib/") [no_ext.sub(%r{\Alib/}, ""), true] else [no_ext, false] end end |
.longer_owner_stems(name, entry) ⇒ Array<String>
Longer basenames that own entry, one per prefix of its pieces.
user_session__test.rb yields nothing for user_session.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/mutineer/pairing.rb', line 726 def longer_owner_stems(name, entry) # Bytes, so a non-UTF-8 name compares without raising (#346). name = name.b raw = entry.b suffix = "_test.rb".b stem = raw.end_with?(suffix) ? raw.byteslice(0, raw.bytesize - suffix.bytesize) : raw needle = name + "_".b return [] unless stem.start_with?(needle) rest = stem.byteslice(needle.bytesize..) return [] if rest.nil? || rest.empty? parts = rest.split("_".b, -1) (1..parts.length).filter_map do |i| longer = name + "_".b + parts.first(i).join("_".b) FsPath.read(longer) if longer_owns_entry?(entry, longer) end end |
.longer_owns_entry?(entry, stem) ⇒ Boolean
True when entry is the exact test, or a split test with a non-empty
piece, for stem. user_session__test.rb is neither for
user_session, so that source does not take it from user.rb.
752 753 754 |
# File 'lib/mutineer/pairing.rb', line 752 def longer_owns_entry?(entry, stem) entry.b == stem.b + "_test.rb".b || split_entry?(entry, stem) end |
.longer_source_exists?(root, dir, stem, scanned_dir = nil) ⇒ Boolean
True when a longer source file exists beside this logical path.
The logical directory itself is checked, so src/user_session.rb owns
test/src/user_session_test.rb. app/ and lib/ are checked too, so an
app/ source still yields the name to a lib/ sibling and the reverse.
766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/mutineer/pairing.rb', line 766 def longer_source_exists?(root, dir, stem, scanned_dir = nil) file = stem.b + ".rb".b folders = [dir == "." ? nil : dir] %w[app lib].each { |prefix| folders << (dir == "." ? prefix : File.join(prefix, dir)) } folders.uniq.any? do |folder| rel = FsPath.read(folder ? folder.b + "/".b + file : file) next false unless File.file?(root.b + "/".b + rel.b) next false if scanned_dir && !source_searches_dir?(rel, scanned_dir) true end end |
.mapped_tests_for(source, resolved) ⇒ Array<String>
Mapped test files for one source from a resolved map. Each entry whose
expanded sources include source contributes its tests, in entry order.
An unmatched source returns []. Does not dedupe across entries; the
CLI unions the run's test list (#382).
156 157 158 159 160 |
# File 'lib/mutineer/pairing.rb', line 156 def mapped_tests_for(source, resolved) Array(resolved[:entries]).each_with_object([]) do |entry, out| out.concat(entry[:tests]) if entry[:sources].include?(source) end end |
.mirror_dir(prefix, base) ⇒ String
Relative test directory for a logical source path. A source with no
subdirectory (calc) maps to prefix itself, not prefix/..
594 595 596 597 |
# File 'lib/mutineer/pairing.rb', line 594 def mirror_dir(prefix, base) dir = File.dirname(base) dir == "." ? prefix : File.join(prefix, dir) end |
.named_test_file(match, root, roots) ⇒ String?
A directory match whose basename is a test name, as a stored path.
230 231 232 233 234 235 236 |
# File 'lib/mutineer/pairing.rb', line 230 def named_test_file(match, root, roots) full = File.absolute_path?(match) ? match : File.(match, root) return unless File.file?(full) return unless File.basename(full).match?(TEST_NAME) existing_relative(full, roots) || full end |
.namespace_base(source_rel) ⇒ String?
Namespace mirror for a concern, or nil.
app/<dir>/concerns/<rest>.rb returns <dir>/<rest>.
concerns must sit directly under app/<dir>.
468 469 470 471 472 473 |
# File 'lib/mutineer/pairing.rb', line 468 def namespace_base(source_rel) match = source_rel.match(%r{\Aapp/([^/]+)/concerns/(.+)\.rb\z}) return unless match "#{match[1]}/#{match[2]}" end |
.namespace_concerns_dir(ns_base) ⇒ String?
app/<dir>/concerns/<rest-dir> for a namespace base <dir>/<rest>.
708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/mutineer/pairing.rb', line 708 def namespace_concerns_dir(ns_base) app_dir, rest = ns_base.split("/", 2) return unless app_dir && rest parent = File.dirname(rest) if parent == "." File.join("app", app_dir, "concerns") else File.join("app", app_dir, "concerns", parent) end end |
.namespace_tests(ns, root, prefer) ⇒ Array<String>
Namespace exact and split tests for a concern whose concerns/ path
has none. A split is left out only when a longer concern has no test
of its own and therefore pairs with that split. A longer model is
already left out by split_test_files. A spec stays the only file.
451 452 453 454 455 456 457 458 459 460 |
# File 'lib/mutineer/pairing.rb', line 451 def namespace_tests(ns, root, prefer) found = tests_at(ns, false, root, prefer) return found if found.any? { |rel| rel.end_with?("_spec.rb") } name = File.basename(ns) found.reject do |rel| entry = File.basename(rel) split_entry?(entry, name) && claimed_by_longer_concern?(root, ns, entry) end end |
.resolve_test_map(test_map, project_root:) ⇒ Hash{Symbol => Array}
Expand every entry of a checked test_map into cleaned file lists.
Source patterns use Dir.glob then expand_sources, so a directory
match becomes its .rb files and ./lib/a.rb / lib//a.rb clean to
the same path. Test patterns go through expand_test; a missing plain
path is dropped (no match). Overlapping patterns keep each file once,
sorted. Patterns with no file are listed under :unmatched with a
1-based entry number and the key ("sources" or "tests"). Does not
print or exit; the CLI reports unmatched patterns (#382).
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mutineer/pairing.rb', line 131 def resolve_test_map(test_map, project_root:) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq unmatched = [] entries = Array(test_map).each_with_index.map do |entry, index| number = index + 1 sources = (Array(entry[:sources]), number, "sources", unmatched) do |pattern| (pattern, root, roots) end tests = (Array(entry[:tests]), number, "tests", unmatched) do |pattern| (pattern, root, roots) end { sources: sources, tests: tests } end { entries: entries, unmatched: unmatched } end |
.root_relative(abs, roots) ⇒ String?
abs relative to the first of roots it is under, or nil. Text only: a
symlinked source keeps its own name (realpath would rename it to its
target and pair the wrong test). roots holds the root's real path too,
so the macOS /var vs /private/var alias still matches.
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/mutineer/pairing.rb', line 376 def root_relative(abs, roots) abs = FsPath.read(abs) roots.each do |r| r = FsPath.read(r) prefix = r.b prefix += "/".b unless prefix.end_with?("/".b) next unless FsPath.bytes_start_with?(abs, prefix) return FsPath.bytes_delete_prefix(abs, prefix) end nil end |
.source_searches_dir?(source_rel, scanned_dir) ⇒ Boolean
True when pairing this source looks in scanned_dir.
784 785 786 787 788 789 |
# File 'lib/mutineer/pairing.rb', line 784 def source_searches_dir?(source_rel, scanned_dir) base, lib = logical_path(source_rel) dirs = [mirror_dir("test", base)] dirs << mirror_dir("test/lib", base) if lib dirs.include?(scanned_dir) end |
.split_entry?(entry, name) ⇒ Boolean
True when entry is <name>_<piece>_test.rb and not the exact
<name>_test.rb file.
644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/mutineer/pairing.rb', line 644 def split_entry?(entry, name) entry = FsPath.read(entry) name = FsPath.read(name) return false unless entry.b.end_with?("_test.rb".b) needle = name.b + "_".b return false unless entry.b.start_with?(needle) rest = entry.b.byteslice(needle.bytesize..) suffix = "_test.rb".b !rest.nil? && rest.bytesize > suffix.bytesize && rest.end_with?(suffix) end |
.split_test_files(base, lib, root) ⇒ Array<String>
<basename>_*_test.rb files in each mirrored test directory (#87). The
exact <basename>_test.rb name is not included. lib/ sources also look
under test/lib/. Names are matched as text, not as a glob, so a basename
that contains * stays literal. test/ comes before test/lib/, and
each directory is sorted.
581 582 583 584 585 586 |
# File 'lib/mutineer/pairing.rb', line 581 def split_test_files(base, lib, root) name = File.basename(base) dirs = [mirror_dir("test", base)] dirs << mirror_dir("test/lib", base) if lib dirs.flat_map { |dir| split_tests_in(root, dir, name, base) } end |
.split_tests_in(root, dir_rel, name, base) ⇒ Array<String>
Split test files directly inside dir_rel (not in subdirectories).
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/mutineer/pairing.rb', line 606 def split_tests_in(root, dir_rel, name, base) root_abs = File.(root) dir_abs = File.(dir_rel, root_abs) return [] unless inside_project?(dir_abs, root_abs) return [] unless File.directory?(dir_abs) fs_children(dir_abs).filter_map do |entry| next unless split_entry?(entry, name) next unless File.file?(dir_abs.b + "/".b + entry.b) next if claimed_by_longer_source?(root, base, entry, dir_rel) FsPath.read(dir_rel == "." ? entry.b : dir_rel.b + "/".b + entry.b) # Byte order: a UTF-8 name and a binary name can both hold bytes # above 127, and String#sort raises on that mix (#346). end.sort_by(&:b) end |
.test_given_as_source?(path, project_root:, tests:) ⇒ Boolean
Whether the run guard rejects a positional argument as a test file
given as a source (#293). Segments are read from the path relative to
the root, so a test/ folder above the project never counts. A
*_test.rb / *_spec.rb path is flagged when it has a test/ or
spec/ segment (the guard's original rule), or when it sits in the same
directory as a given test, which catches root-level tests. A
test_*.rb path is flagged only under the
project's top-level test/ or spec/, because real sources use the
prefix (lib/mutineer/test_runners.rb, lib/foo/test/test_app.rb). A
source whose own test (<stem>_test.rb, test_<stem>.rb,
<stem>_spec.rb, or a split <stem>_*_test.rb not claimed by a longer
source on disk) sits beside it in the test set is not flagged. Paths
compare relative to the root, as in
expand_sources (the root's real path counts too, for macOS /var vs
/private/var), so ./a_test.rb and a_test.rb match. No file content
is read.
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/mutineer/pairing.rb', line 538 def test_given_as_source?(path, project_root:, tests:) name = File.basename(path) return false unless name.match?(TEST_NAME) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq relative = lambda do |p| abs = File.(p, root) root_relative(abs, roots) || abs end rel = relative.call(path) # Outside the root, read segments from a relative path as typed # (`../test/b_test.rb`) and not from an absolute one, so a `test/` # folder above the root never counts. segments = root_relative(File.(path, root), roots) segments ||= path.start_with?(File::SEPARATOR) ? "" : path return segments.match?(%r{\A(test|spec)/}) unless name.match?(/_(test|spec)\.rb\z/) return true if segments.match?(%r{(\A|/)(test|spec)/}) dir = File.dirname(rel) beside = Array(tests).map(&relative) .select { |t| File.dirname(t) == dir } .map { |t| File.basename(t) } stem = File.basename(name, ".rb") own = ["test_#{stem}.rb", "#{stem}_spec.rb"] base = rel.delete_suffix(".rb") return false if beside.any? do |t| own.include?(t) || (longer_owns_entry?(t, stem) && !claimed_by_longer_source?(root, base, t)) end beside.any? end |
.tests_at(base, lib, root, prefer) ⇒ Array<String>
Exact and split tests for one logical base. A spec stays the only file.
435 436 437 438 439 440 |
# File 'lib/mutineer/pairing.rb', line 435 def tests_at(base, lib, root, prefer) exact = candidates(base, lib, prefer).find { |rel| File.exist?(File.(rel, root)) } return [exact] if exact&.end_with?("_spec.rb") ([exact] + split_test_files(base, lib, root)).compact.uniq end |
Instance Method Details
#candidates(base, lib, prefer) ⇒ Array<String> (private)
Ordered candidate test paths: test.rb, then Minitest's test*.rb, then _spec.rb. lib/ sources also get the test/lib/... and spec/lib/... layouts.
500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/mutineer/pairing.rb', line 500 def candidates(base, lib, prefer) minitest = ["test/#{base}_test.rb"] minitest << "test/lib/#{base}_test.rb" if lib unless File.basename(base) == "helper" # test/test_helper.rb is Minitest's support file, not a test prefixed = base.sub(%r{[^/]+\z}) { |name| "test_#{name}" } minitest << "test/#{prefixed}.rb" minitest << "test/lib/#{prefixed}.rb" if lib end rspec = ["spec/#{base}_spec.rb"] rspec << "spec/lib/#{base}_spec.rb" if lib prefer == "rspec" ? rspec + minitest : minitest + rspec end |
#claimed_by_longer_concern?(root, ns_base, entry) ⇒ Boolean (private)
True when a longer concern pairs with this split name.
The file sits under app/<dir>/concerns/, a directory
longer_source_exists? does not search for a namespace base.
The longer concern claims the name only when its concerns/ path has
no test, because only then does it look at the namespace path.
A model keeps a split file it already pairs with (#408).
688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/mutineer/pairing.rb', line 688 def claimed_by_longer_concern?(root, ns_base, entry) dir = namespace_concerns_dir(ns_base) return false unless dir longer_owner_stems(File.basename(ns_base), entry).any? do |longer| rel = File.join(dir, "#{longer}.rb") abs = File.(rel, root) # A source path with `..` must not make this look at a file outside the project. next false unless inside_project?(abs, root) && File.file?(abs) base, lib = logical_path(rel) tests_at(base, lib, root, "minitest").empty? end end |
#claimed_by_longer_source?(root, base, entry, scanned_dir = nil) ⇒ Boolean (private)
True when a longer source file on disk owns this split test. user.rb
does not take user_session_test.rb when user_session.rb exists in the
same directory, under app/, or under lib/. An intermediate file
counts: bar_upsert.rb owns bar_upsert_guards_test.rb, so bar.rb
does not.
670 671 672 673 674 675 |
# File 'lib/mutineer/pairing.rb', line 670 def claimed_by_longer_source?(root, base, entry, scanned_dir = nil) dir = File.dirname(base) longer_owner_stems(File.basename(base), entry).any? do |longer| longer_source_exists?(root, dir, longer, scanned_dir) end end |
#classify_test_value(value, project_root:) ⇒ Symbol (private)
How one --test value is read. Blank is not a directory: File.expand_path of "" is the project root, and that would select every test.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mutineer/pairing.rb', line 84 def classify_test_value(value, project_root:) return :blank if value.nil? || value.strip.empty? abs = File.(value, project_root) return :directory if File.directory?(abs) return :file if File.exist?(abs) return :glob if value.match?(TEST_GLOB_MARK) :plain end |
#directory_test_files(abs, root, roots) ⇒ Array<String> (private)
Test files under a directory. The name rule is TEST_NAME. Dot entries stay out, because Dir.glob skips them. The result is sorted. The directory name is literal, even when it contains a pattern character.
198 199 200 201 202 203 204 205 |
# File 'lib/mutineer/pairing.rb', line 198 def directory_test_files(abs, root, roots) listed = if (rel = root_relative(abs, roots)) fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root) else fs_glob(File.join(glob_literal(abs), "**", "*.rb")) end listed.filter_map { |match| named_test_file(match, root, roots) }.sort_by(&:b) end |
#existing_relative(abs, roots) ⇒ String? (private)
An existing file's root-relative path, or nil outside the root. Tries the
path as given, then with its directory resolved, so a path typed through
a symlinked parent (or macOS /var under a /private/var root) matches.
The file's own name is kept, so a symlinked file is not renamed.
363 364 365 366 |
# File 'lib/mutineer/pairing.rb', line 363 def existing_relative(abs, roots) root_relative(abs, roots) || root_relative(File.join(File.realpath(File.dirname(abs)), File.basename(abs)), roots) end |
#expand_map_patterns(patterns, number, key, unmatched) {|pattern| ... } ⇒ Array<String> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Expand each pattern for one map key. An empty expansion records the
pattern under unmatched. Results are deduped and sorted.
262 263 264 265 266 267 268 |
# File 'lib/mutineer/pairing.rb', line 262 def (patterns, number, key, unmatched) patterns.flat_map do |pattern| files = yield(pattern) unmatched << { entry: number, key: key, pattern: pattern } if files.empty? files end.uniq.sort_by(&:b) end |
#expand_map_source_pattern(pattern, root, roots) ⇒ Array<String> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
One map source pattern: Dir.glob then expand_sources, so a matched
directory becomes its .rb files and path spellings clean the same way.
Matches outside the project root are dropped (brace patterns like
{..,app}/x.rb can escape a text-only .. check).
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/mutineer/pairing.rb', line 280 def (pattern, root, roots) # An existing file or directory is literal, even when its name holds a # glob character (same rule as {classify_test_value} for --test). abs = File.(pattern, root) listed = if File.exist?(abs) [pattern] else fs_glob(pattern, base: root) end return [] if listed.empty? (listed, project_root: root).filter_map do |path| path_abs = File.(path, root) next unless File.exist?(path_abs) existing_relative(path_abs, roots) end.uniq.sort_by(&:b) end |
#expand_map_test_glob(pattern, root, roots) ⇒ Array<String> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Glob matches for a map tests: pattern. A matched directory expands with
directory_test_files; a matched file is kept as-is (no name filter).
--test globs still drop directories; only the map expands them (#382).
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/mutineer/pairing.rb', line 337 def (pattern, root, roots) matches = if File.absolute_path?(pattern) fs_glob(pattern) else fs_glob(pattern, base: root) end matches.flat_map do |match| full = File.absolute_path?(match) ? match : File.(match, root) if File.directory?(full) directory_test_files(full, root, roots) elsif File.file?(full) [existing_relative(full, roots) || full] else [] end end end |
#expand_map_test_pattern(pattern, root, roots) ⇒ Array<String> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
One map test pattern. Directories (typed or matched by a glob) expand
with the TEST_NAME filter, matching a typed --test directory. Files
a glob matches stay unfiltered (including test_helper.rb), matching
--test globs. A missing plain path is not a match. Matches outside the
project root are dropped (#382 review).
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/mutineer/pairing.rb', line 310 def (pattern, root, roots) listed = case classify_test_value(pattern, project_root: root) when :directory directory_test_files(File.(pattern, root), root, roots) when :glob (pattern, root, roots) else # :file / :plain / :blank — same as {expand_test}, then root-filter. (pattern, project_root: root) end listed.filter_map do |path| abs = File.(path, root) next unless File.file?(abs) existing_relative(abs, roots) end.uniq.sort_by(&:b) end |
#expand_sources(args, project_root:) ⇒ Array<String> (private)
Expand each positional source: a directory -> its sorted **/*.rb files
(relative to project_root); a file (or glob, or anything non-directory)
-> its path relative to project_root, so ./lib/x.rb and an absolute
path pair like lib/x.rb (#104). A missing file, or one outside the
root, stays as typed. Flattened, deduped, order-stable.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mutineer/pairing.rb', line 47 def (args, project_root:) root = FsPath.read(File.(project_root)) # The root as typed and as its real path (macOS `/var` vs `/private/var`), # resolved once for every file below. roots = [root, ProjectPath.root_real(root)].uniq Array(args).flat_map do |arg| arg = FsPath.read(arg) abs = File.(arg, root) if File.directory?(abs) # Escape metacharacters in the directory name so `component[1]` stays # literal (same rule as {directory_test_files}). listed = if (rel = root_relative(abs, roots)) fs_glob(File.join(glob_literal(rel), "**", "*.rb"), base: root) else fs_glob(File.join(glob_literal(abs), "**", "*.rb")) end listed.sort_by(&:b).map do |f| full = File.absolute_path?(f) ? f : File.(f, root) existing_relative(full, roots) || full end else # `..` resolves as text, as FileSwap and the daemon already read it. [(existing_relative(abs, roots) if File.exist?(abs)) || arg] end end.map { |path| FsPath.read(path) }.uniq end |
#expand_test(value, project_root:) ⇒ Array<String> (private)
One --test value, as paths. [] means a directory or glob matched nothing. A file stays as typed. A missing plain path stays as typed, so the CLI can still say "no such file". Paths from a directory or glob are sorted. Inside the project they are root-relative. Outside it they are absolute.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mutineer/pairing.rb', line 103 def (value, project_root:) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq case classify_test_value(value, project_root: project_root) when :directory directory_test_files(File.(value, root), root, roots) when :glob glob_test_files(value, root, roots) else [value] end end |
#fs_children(dir) ⇒ Array<String> (private)
Dir.children with each name read as UTF-8, or binary when it is not (#346).
186 187 188 |
# File 'lib/mutineer/pairing.rb', line 186 def fs_children(dir) Dir.children(dir).map { |path| FsPath.read(path) } end |
#fs_glob(*args, **kwargs) ⇒ Array<String> (private)
Dir.glob with each path read as UTF-8, or binary when it is not (#346).
178 179 180 |
# File 'lib/mutineer/pairing.rb', line 178 def fs_glob(*args, **kwargs) Dir.glob(*args, **kwargs).map { |path| FsPath.read(path) } end |
#glob_file(match, root, roots) ⇒ String? (private)
A glob match that is a file, as a stored path. Directories are dropped.
244 245 246 247 248 249 |
# File 'lib/mutineer/pairing.rb', line 244 def glob_file(match, root, roots) full = File.absolute_path?(match) ? match : File.(match, root) return unless File.file?(full) existing_relative(full, roots) || full end |
#glob_literal(path) ⇒ String (private)
A path used as a literal prefix in Dir.glob.
170 171 172 |
# File 'lib/mutineer/pairing.rb', line 170 def glob_literal(path) FsPath.read(path).gsub(GLOB_META) { "\\#{Regexp.last_match(1)}" } end |
#glob_test_files(pattern, root, roots) ⇒ Array<String> (private)
Files a glob matches from the project root. Directories are ignored. There is no test-name filter. An absolute pattern ignores base. Dir.glob keeps a leading "./"; the path is then made root-relative.
215 216 217 218 219 220 221 222 |
# File 'lib/mutineer/pairing.rb', line 215 def glob_test_files(pattern, root, roots) listed = if File.absolute_path?(pattern) fs_glob(pattern) else fs_glob(pattern, base: root) end listed.filter_map { |match| glob_file(match, root, roots) }.sort_by(&:b) end |
#infer_test(source_rel, project_root:, prefer: "minitest") ⇒ String? (private)
The first test path #infer_tests would run for a source, or nil.
prefer is the resolved framework ("minitest" | "rspec"): its candidates
are tried first, the other framework's as fallback, so a minitest default
still finds a spec and vice-versa.
398 399 400 |
# File 'lib/mutineer/pairing.rb', line 398 def infer_test(source_rel, project_root:, prefer: "minitest") infer_tests(source_rel, project_root: project_root, prefer: prefer).first end |
#infer_tests(source_rel, project_root:, prefer: "minitest") ⇒ Array<String> (private)
Every test file convention pairs with a source. The first existing exact
candidate from #candidates stays first. Each <basename>_*_test.rb in
the mirrored test directory is added after it (#87). A spec that the exact
rules already found is left alone: split Minitest names do not join it
and do not replace it. When that list is empty, a concern tries the same
exact and split names at its namespace path (#408). The concerns/ path
still wins when it has any test. A longer source, including a longer
concern, keeps a split file. A model keeps a split file it already pairs
with: this fallback does not take one away.
416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/mutineer/pairing.rb', line 416 def infer_tests(source_rel, project_root:, prefer: "minitest") base, lib = logical_path(source_rel) root = File.(project_root) found = tests_at(base, lib, root, prefer) return found unless found.empty? ns = namespace_base(source_rel) return found unless ns namespace_tests(ns, root, prefer) end |
#inside_project?(dir_abs, root_abs) ⇒ Boolean (private)
True when dir_abs is the project root or a directory inside it.
A source path such as ../../evil.rb must not list tests outside the project.
629 630 631 632 633 634 635 636 |
# File 'lib/mutineer/pairing.rb', line 629 def inside_project?(dir_abs, root_abs) dir_b = FsPath.read(dir_abs).b root_b = FsPath.read(root_abs).b return true if dir_b == root_b prefix = root_b == "/".b ? "/".b : root_b + "/".b FsPath.bytes_start_with?(dir_b, prefix) end |
#logical_path(source_rel) ⇒ Array(String, Boolean) (private)
Strip the source root to a logical path (no ".rb") and flag lib/ sources. app/foo/bar.rb and lib/foo/bar.rb both -> "foo/bar"; anything else -> the path minus ".rb" (still attempted). Namespaced subdirs are preserved verbatim — structural, never constant resolution.
482 483 484 485 486 487 488 489 490 491 |
# File 'lib/mutineer/pairing.rb', line 482 def logical_path(source_rel) no_ext = source_rel.sub(/\.rb\z/, "") if no_ext.start_with?("app/") [no_ext.sub(%r{\Aapp/}, ""), false] elsif no_ext.start_with?("lib/") [no_ext.sub(%r{\Alib/}, ""), true] else [no_ext, false] end end |
#longer_owner_stems(name, entry) ⇒ Array<String> (private)
Longer basenames that own entry, one per prefix of its pieces.
user_session__test.rb yields nothing for user_session.
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/mutineer/pairing.rb', line 726 def longer_owner_stems(name, entry) # Bytes, so a non-UTF-8 name compares without raising (#346). name = name.b raw = entry.b suffix = "_test.rb".b stem = raw.end_with?(suffix) ? raw.byteslice(0, raw.bytesize - suffix.bytesize) : raw needle = name + "_".b return [] unless stem.start_with?(needle) rest = stem.byteslice(needle.bytesize..) return [] if rest.nil? || rest.empty? parts = rest.split("_".b, -1) (1..parts.length).filter_map do |i| longer = name + "_".b + parts.first(i).join("_".b) FsPath.read(longer) if longer_owns_entry?(entry, longer) end end |
#longer_owns_entry?(entry, stem) ⇒ Boolean (private)
True when entry is the exact test, or a split test with a non-empty
piece, for stem. user_session__test.rb is neither for
user_session, so that source does not take it from user.rb.
752 753 754 |
# File 'lib/mutineer/pairing.rb', line 752 def longer_owns_entry?(entry, stem) entry.b == stem.b + "_test.rb".b || split_entry?(entry, stem) end |
#longer_source_exists?(root, dir, stem, scanned_dir = nil) ⇒ Boolean (private)
True when a longer source file exists beside this logical path.
The logical directory itself is checked, so src/user_session.rb owns
test/src/user_session_test.rb. app/ and lib/ are checked too, so an
app/ source still yields the name to a lib/ sibling and the reverse.
766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/mutineer/pairing.rb', line 766 def longer_source_exists?(root, dir, stem, scanned_dir = nil) file = stem.b + ".rb".b folders = [dir == "." ? nil : dir] %w[app lib].each { |prefix| folders << (dir == "." ? prefix : File.join(prefix, dir)) } folders.uniq.any? do |folder| rel = FsPath.read(folder ? folder.b + "/".b + file : file) next false unless File.file?(root.b + "/".b + rel.b) next false if scanned_dir && !source_searches_dir?(rel, scanned_dir) true end end |
#mapped_tests_for(source, resolved) ⇒ Array<String> (private)
Mapped test files for one source from a resolved map. Each entry whose
expanded sources include source contributes its tests, in entry order.
An unmatched source returns []. Does not dedupe across entries; the
CLI unions the run's test list (#382).
156 157 158 159 160 |
# File 'lib/mutineer/pairing.rb', line 156 def mapped_tests_for(source, resolved) Array(resolved[:entries]).each_with_object([]) do |entry, out| out.concat(entry[:tests]) if entry[:sources].include?(source) end end |
#mirror_dir(prefix, base) ⇒ String (private)
Relative test directory for a logical source path. A source with no
subdirectory (calc) maps to prefix itself, not prefix/..
594 595 596 597 |
# File 'lib/mutineer/pairing.rb', line 594 def mirror_dir(prefix, base) dir = File.dirname(base) dir == "." ? prefix : File.join(prefix, dir) end |
#named_test_file(match, root, roots) ⇒ String? (private)
A directory match whose basename is a test name, as a stored path.
230 231 232 233 234 235 236 |
# File 'lib/mutineer/pairing.rb', line 230 def named_test_file(match, root, roots) full = File.absolute_path?(match) ? match : File.(match, root) return unless File.file?(full) return unless File.basename(full).match?(TEST_NAME) existing_relative(full, roots) || full end |
#namespace_base(source_rel) ⇒ String? (private)
Namespace mirror for a concern, or nil.
app/<dir>/concerns/<rest>.rb returns <dir>/<rest>.
concerns must sit directly under app/<dir>.
468 469 470 471 472 473 |
# File 'lib/mutineer/pairing.rb', line 468 def namespace_base(source_rel) match = source_rel.match(%r{\Aapp/([^/]+)/concerns/(.+)\.rb\z}) return unless match "#{match[1]}/#{match[2]}" end |
#namespace_concerns_dir(ns_base) ⇒ String? (private)
app/<dir>/concerns/<rest-dir> for a namespace base <dir>/<rest>.
708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/mutineer/pairing.rb', line 708 def namespace_concerns_dir(ns_base) app_dir, rest = ns_base.split("/", 2) return unless app_dir && rest parent = File.dirname(rest) if parent == "." File.join("app", app_dir, "concerns") else File.join("app", app_dir, "concerns", parent) end end |
#namespace_tests(ns, root, prefer) ⇒ Array<String> (private)
Namespace exact and split tests for a concern whose concerns/ path
has none. A split is left out only when a longer concern has no test
of its own and therefore pairs with that split. A longer model is
already left out by split_test_files. A spec stays the only file.
451 452 453 454 455 456 457 458 459 460 |
# File 'lib/mutineer/pairing.rb', line 451 def namespace_tests(ns, root, prefer) found = tests_at(ns, false, root, prefer) return found if found.any? { |rel| rel.end_with?("_spec.rb") } name = File.basename(ns) found.reject do |rel| entry = File.basename(rel) split_entry?(entry, name) && claimed_by_longer_concern?(root, ns, entry) end end |
#resolve_test_map(test_map, project_root:) ⇒ Hash{Symbol => Array} (private)
Expand every entry of a checked test_map into cleaned file lists.
Source patterns use Dir.glob then expand_sources, so a directory
match becomes its .rb files and ./lib/a.rb / lib//a.rb clean to
the same path. Test patterns go through expand_test; a missing plain
path is dropped (no match). Overlapping patterns keep each file once,
sorted. Patterns with no file are listed under :unmatched with a
1-based entry number and the key ("sources" or "tests"). Does not
print or exit; the CLI reports unmatched patterns (#382).
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mutineer/pairing.rb', line 131 def resolve_test_map(test_map, project_root:) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq unmatched = [] entries = Array(test_map).each_with_index.map do |entry, index| number = index + 1 sources = (Array(entry[:sources]), number, "sources", unmatched) do |pattern| (pattern, root, roots) end tests = (Array(entry[:tests]), number, "tests", unmatched) do |pattern| (pattern, root, roots) end { sources: sources, tests: tests } end { entries: entries, unmatched: unmatched } end |
#root_relative(abs, roots) ⇒ String? (private)
abs relative to the first of roots it is under, or nil. Text only: a
symlinked source keeps its own name (realpath would rename it to its
target and pair the wrong test). roots holds the root's real path too,
so the macOS /var vs /private/var alias still matches.
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/mutineer/pairing.rb', line 376 def root_relative(abs, roots) abs = FsPath.read(abs) roots.each do |r| r = FsPath.read(r) prefix = r.b prefix += "/".b unless prefix.end_with?("/".b) next unless FsPath.bytes_start_with?(abs, prefix) return FsPath.bytes_delete_prefix(abs, prefix) end nil end |
#source_searches_dir?(source_rel, scanned_dir) ⇒ Boolean (private)
True when pairing this source looks in scanned_dir.
784 785 786 787 788 789 |
# File 'lib/mutineer/pairing.rb', line 784 def source_searches_dir?(source_rel, scanned_dir) base, lib = logical_path(source_rel) dirs = [mirror_dir("test", base)] dirs << mirror_dir("test/lib", base) if lib dirs.include?(scanned_dir) end |
#split_entry?(entry, name) ⇒ Boolean (private)
True when entry is <name>_<piece>_test.rb and not the exact
<name>_test.rb file.
644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/mutineer/pairing.rb', line 644 def split_entry?(entry, name) entry = FsPath.read(entry) name = FsPath.read(name) return false unless entry.b.end_with?("_test.rb".b) needle = name.b + "_".b return false unless entry.b.start_with?(needle) rest = entry.b.byteslice(needle.bytesize..) suffix = "_test.rb".b !rest.nil? && rest.bytesize > suffix.bytesize && rest.end_with?(suffix) end |
#split_test_files(base, lib, root) ⇒ Array<String> (private)
<basename>_*_test.rb files in each mirrored test directory (#87). The
exact <basename>_test.rb name is not included. lib/ sources also look
under test/lib/. Names are matched as text, not as a glob, so a basename
that contains * stays literal. test/ comes before test/lib/, and
each directory is sorted.
581 582 583 584 585 586 |
# File 'lib/mutineer/pairing.rb', line 581 def split_test_files(base, lib, root) name = File.basename(base) dirs = [mirror_dir("test", base)] dirs << mirror_dir("test/lib", base) if lib dirs.flat_map { |dir| split_tests_in(root, dir, name, base) } end |
#split_tests_in(root, dir_rel, name, base) ⇒ Array<String> (private)
Split test files directly inside dir_rel (not in subdirectories).
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/mutineer/pairing.rb', line 606 def split_tests_in(root, dir_rel, name, base) root_abs = File.(root) dir_abs = File.(dir_rel, root_abs) return [] unless inside_project?(dir_abs, root_abs) return [] unless File.directory?(dir_abs) fs_children(dir_abs).filter_map do |entry| next unless split_entry?(entry, name) next unless File.file?(dir_abs.b + "/".b + entry.b) next if claimed_by_longer_source?(root, base, entry, dir_rel) FsPath.read(dir_rel == "." ? entry.b : dir_rel.b + "/".b + entry.b) # Byte order: a UTF-8 name and a binary name can both hold bytes # above 127, and String#sort raises on that mix (#346). end.sort_by(&:b) end |
#test_given_as_source?(path, project_root:, tests:) ⇒ Boolean (private)
Whether the run guard rejects a positional argument as a test file
given as a source (#293). Segments are read from the path relative to
the root, so a test/ folder above the project never counts. A
*_test.rb / *_spec.rb path is flagged when it has a test/ or
spec/ segment (the guard's original rule), or when it sits in the same
directory as a given test, which catches root-level tests. A
test_*.rb path is flagged only under the
project's top-level test/ or spec/, because real sources use the
prefix (lib/mutineer/test_runners.rb, lib/foo/test/test_app.rb). A
source whose own test (<stem>_test.rb, test_<stem>.rb,
<stem>_spec.rb, or a split <stem>_*_test.rb not claimed by a longer
source on disk) sits beside it in the test set is not flagged. Paths
compare relative to the root, as in
expand_sources (the root's real path counts too, for macOS /var vs
/private/var), so ./a_test.rb and a_test.rb match. No file content
is read.
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/mutineer/pairing.rb', line 538 def test_given_as_source?(path, project_root:, tests:) name = File.basename(path) return false unless name.match?(TEST_NAME) root = File.(project_root) roots = [root, ProjectPath.root_real(root)].uniq relative = lambda do |p| abs = File.(p, root) root_relative(abs, roots) || abs end rel = relative.call(path) # Outside the root, read segments from a relative path as typed # (`../test/b_test.rb`) and not from an absolute one, so a `test/` # folder above the root never counts. segments = root_relative(File.(path, root), roots) segments ||= path.start_with?(File::SEPARATOR) ? "" : path return segments.match?(%r{\A(test|spec)/}) unless name.match?(/_(test|spec)\.rb\z/) return true if segments.match?(%r{(\A|/)(test|spec)/}) dir = File.dirname(rel) beside = Array(tests).map(&relative) .select { |t| File.dirname(t) == dir } .map { |t| File.basename(t) } stem = File.basename(name, ".rb") own = ["test_#{stem}.rb", "#{stem}_spec.rb"] base = rel.delete_suffix(".rb") return false if beside.any? do |t| own.include?(t) || (longer_owns_entry?(t, stem) && !claimed_by_longer_source?(root, base, t)) end beside.any? end |
#tests_at(base, lib, root, prefer) ⇒ Array<String> (private)
Exact and split tests for one logical base. A spec stays the only file.
435 436 437 438 439 440 |
# File 'lib/mutineer/pairing.rb', line 435 def tests_at(base, lib, root, prefer) exact = candidates(base, lib, prefer).find { |rel| File.exist?(File.(rel, root)) } return [exact] if exact&.end_with?("_spec.rb") ([exact] + split_test_files(base, lib, root)).compact.uniq end |