Skip to content

Commit 9b322b0

Browse files
committed
Allow to test a custom fixtures path during testing
Of course, these won't really be fixtures, but it allows to test against whole codebases without copying them, doing symlinks or something like that. For example, I can tell that over the whole RuboCop codebase, there are only 8 files that produce mismatched ast. Telling what the problem is is a different problem. The ast for real files can and will be huge so I haven't checked yet (maybe parser bug) but it's nice for discoverability regardless
1 parent de7bb68 commit 9b322b0

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

test/prism/ruby/parser_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def assert_equal_parses(fixture, compare_asts: true, compare_tokens: true, compa
167167
ignore_warnings { Prism::Translation::Parser33.new.tokenize(buffer) }
168168

169169
if expected_ast == actual_ast
170-
if !compare_asts
170+
if !compare_asts && !Fixture.custom_base_path?
171171
puts "#{fixture.path} is now passing"
172172
end
173173

@@ -178,7 +178,7 @@ def assert_equal_parses(fixture, compare_asts: true, compare_tokens: true, compa
178178
rescue Test::Unit::AssertionFailedError
179179
raise if compare_tokens
180180
else
181-
puts "#{fixture.path} is now passing" if !compare_tokens
181+
puts "#{fixture.path} is now passing" if !compare_tokens && !Fixture.custom_base_path?
182182
end
183183

184184
assert_equal_comments(expected_comments, actual_comments) if compare_comments

test/prism/snapshots_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
require_relative "test_helper"
44

5+
# We don't want to generate snapshots when running against files
6+
# outside of the project folder.
7+
return if Prism::TestCase::Fixture.custom_base_path?
8+
59
module Prism
610
class SnapshotsTest < TestCase
711
# When we pretty-print the trees to compare against the snapshots, we want

test/prism/test_helper.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TestCase < ::Test::Unit::TestCase
3838
# are used to define test methods that assert against each fixture in some
3939
# way.
4040
class Fixture
41-
BASE = File.join(__dir__, "fixtures")
41+
BASE = ENV.fetch("FIXTURE_BASE", File.join(__dir__, "fixtures"))
4242

4343
attr_reader :path
4444

@@ -63,9 +63,14 @@ def test_name
6363
end
6464

6565
def self.each(except: [], &block)
66-
paths = Dir[ENV.fetch("FOCUS") { File.join("**", "*.txt") }, base: BASE] - except
66+
glob_pattern = ENV.fetch("FOCUS") { custom_base_path? ? File.join("**", "*.rb") : File.join("**", "*.txt") }
67+
paths = Dir[glob_pattern, base: BASE] - except
6768
paths.each { |path| yield Fixture.new(path) }
6869
end
70+
71+
def self.custom_base_path?
72+
ENV.key?("FIXTURE_BASE")
73+
end
6974
end
7075

7176
# Yield each encoding that we want to test, along with a range of the

0 commit comments

Comments
 (0)