Skip to content

Commit

Permalink
Hotfix Reporters (#5)
Browse files Browse the repository at this point in the history
* Hotfix file write to use reporter
* Adapt simplecov config
  • Loading branch information
unused authored Oct 28, 2024
1 parent 9e1ef37 commit 4eb6203
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

SimpleCov.start do
add_filter 'test/'
add_filter '/test/'
enable_coverage :branch
end
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ $ TEST_MAP=1 bundle exec rspec
On demand you can adapt the configuration to your needs.

```ruby
TestMap::Configure.configure do |config|
config.logger = Logger.new($stdout) # default logs to dev/null
config.out_file = 'my-test-map.yml' # default is .test-map.yml
TestMap::Config.configure do |config|
config[:logger] = Logger.new($stdout) # default logs to dev/null
config[:merge] = false # merge results (e.g. with multiple testsuites)
config[:out_file] = 'my-test-map.yml' # default is .test-map.yml
# defaults to [%r{^(vendor)/}] }
config.exclude_patterns = [%r{^(vendor|other_libraries)/}]
config[:exclude_patterns] = [%r{^(vendor|other_libraries)/}]
# register a custom rule to match new files; must implement `call(file)`;
# defaults to nil
config.natural_mapping = ->(file) { file.sub(%r{^library/}, 'test/') }
config[:natural_mapping] = ->(file) { file.sub(%r{^library/}, 'test/') }
end
```

Expand Down
3 changes: 1 addition & 2 deletions lib/test_map/plugins/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module Minitest
def self.included(_base)
TestMap.logger.info 'Registering hooks for Minitest'
::Minitest.after_run do
result = TestMap.reporter.to_yaml
File.write "#{Dir.pwd}/#{Config.config[:out_file]}", result
TestMap.reporter.write "#{Dir.pwd}/#{Config.config[:out_file]}"
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/test_map/plugins/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
end

config.after(:suite) do
result = TestMap.reporter.to_yaml
File.write "#{Dir.pwd}/#{TestMap::Config.config[:out_file]}", result
TestMap.reporter.write "#{Dir.pwd}/#{TestMap::Config.config[:out_file]}"
end
end
17 changes: 13 additions & 4 deletions lib/test_map/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ def add(files)
end

def write(file)
result = to_yaml
result = YAML.safe_load_file(file).deep_merge(result) if Config.config[:merge]
File.write file, result
content = if File.exist?(file) && Config.config[:merge]
merge(results, YAML.safe_load_file(file)).to_yaml
else
to_yaml
end
File.write file, content
end

def results = @results.transform_values { _1.to_a.sort }.sort.to_h
def results = @results.transform_values { _1.to_a.uniq.sort }.sort.to_h
def to_yaml = results.to_yaml

def merge(result, current)
current.merge(result) do |_key, oldval, newval|
(oldval + newval).uniq.sort
end
end
end
end
6 changes: 5 additions & 1 deletion test/test_map/config_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'test_helper'
require './test/test_helper'

# Tests for configuration.
class ConfigTest < Minitest::Test
Expand All @@ -20,6 +20,10 @@ def test_ensure_default_keys
assert_equal expected_keys, subject.config.keys
end

def test_shorthand_access
assert_equal '.test-map.yml', subject[:out_file]
end

private

def subject = TestMap::Config
Expand Down
2 changes: 1 addition & 1 deletion test/test_map/errors_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'test_helper'
require './test/test_helper'

# Tests for trace in use error.
class TraceInUseErrorTest < Minitest::Test
Expand Down
4 changes: 2 additions & 2 deletions test/test_map/file_recorder_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'test_helper'
require 'fixtures/sample'
require './test/test_helper'
require './test/fixtures/sample'

# Tests for file recorder.
class FileRecorderTest < Minitest::Test
Expand Down
2 changes: 2 additions & 0 deletions test/test_map/filter_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require './test/test_helper'

# Tests for filter class
class FilterTest < Minitest::Test
def test_call
Expand Down
2 changes: 1 addition & 1 deletion test/test_map/mapping_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'test_helper'
require './test/test_helper'

# Tests for mapping.
class MappingTest < Minitest::Test
Expand Down
6 changes: 5 additions & 1 deletion test/test_map/natural_mapping_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'test_helper'
require './test/test_helper'

# Tests for mapping.
class NaturalMappingTest < Minitest::Test
Expand Down Expand Up @@ -37,6 +37,10 @@ def test_no_known_test_lib
end
end

def test_has_registered_default_rules
assert_predicate subject.registered_rules, :any?
end

private

def subject = TestMap::NaturalMapping
Expand Down
11 changes: 10 additions & 1 deletion test/test_map/report_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# frozen_string_literal: true

require 'test_helper'
require './test/test_helper'

# Tests for trace in use error.
class ReportTest < Minitest::Test
def test_empty_results
assert_empty described_class.new.results
end

def test_merges_two_hashes
report = described_class.new
result = report.merge({ a: [1, 2, 3], b: [4, 5, 6] },
{ a: [3, 4, 5], c: [6, 7, 8] })
expected_result = { a: [1, 2, 3, 4, 5], b: [4, 5, 6], c: [6, 7, 8] }

assert_equal expected_result, result
end

private

def described_class = TestMap::Report
Expand Down

0 comments on commit 4eb6203

Please sign in to comment.