-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the new
CIRecordHelper
behaviour and an associate rake task:
- Recording the deprecations whitelist for thousands of tests is a real pain depending on how your CI is setup. Most CI runs test in parallel in different machines, the regular `Record` Behaviour from this gem will create bunch of YAML files, but since multiple test from the same test file runs on different machines, you end up with multiple yaml files for the same test suite. You'd need a way to download all artifacts from CI and "merge" all files, it's a nightmare. This PR introduce a new behaviour that leverage a feature that most CI have which is to download the log output from the test run. The `CIRecordHelper` behaviour will output a JSON representation of your deprecations. You'll simply have to download the log output locally and finally run the associate rake task meant to parse this log file and generate proper deprecation as YAML file.
- Loading branch information
1 parent
8dd82f5
commit acfb897
Showing
7 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
|
||
module DeprecationToolkit | ||
module Behaviors | ||
class CIRecordHelper | ||
extend ReadWriteHelper | ||
|
||
HEADER = '[DeprecationToolkit]' | ||
|
||
def self.trigger(test, current_deprecations, recorded_deprecations) | ||
filename = recorded_deprecations_path(test) | ||
|
||
to_output = { | ||
filename.to_s => { | ||
test.name => current_deprecations.deprecations_without_stacktrace | ||
} | ||
} | ||
|
||
raise "#{HEADER} #{JSON.dump(to_output)}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'tempfile' | ||
require 'json' | ||
require 'active_support/core_ext/hash' | ||
require 'rake' | ||
require_relative '../deprecation_toolkit/read_write_helper' | ||
|
||
class CIRecorder | ||
include Rake::DSL | ||
include DeprecationToolkit::ReadWriteHelper | ||
|
||
def initialize | ||
namespace :deprecation_toolkit do | ||
desc 'Parse a file generated with the CIOutputHelper and generate deprecations out of it' | ||
task :record_from_ci_output do | ||
raw_file = ENV.fetch('FILEPATH') | ||
|
||
deprecations = extract_deprecations_output(raw_file) do |file| | ||
parse_file(file) | ||
end | ||
|
||
generate_deprecations_file(deprecations) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def extract_deprecations_output(file) | ||
tmp_file = Tempfile.new | ||
shell_command = "cat #{file} | sed -n -e 's/^.* \\[DeprecationToolkit\\] \\(.*\\)/\\1/p' > #{tmp_file.path}" | ||
|
||
raise "Couldn't extract deprecations from output" unless system(shell_command) | ||
yield(tmp_file) | ||
ensure | ||
tmp_file.delete | ||
end | ||
|
||
def parse_file(file) | ||
file.each.with_object({}) do |line, hash| | ||
hash.deep_merge!(JSON.parse(line)) | ||
end | ||
end | ||
|
||
def generate_deprecations_file(deprecations_to_record) | ||
deprecations_to_record.each do |filename, deprecations| | ||
write(Pathname(filename), deprecations) | ||
end | ||
end | ||
end | ||
|
||
CIRecorder.new |
76 changes: 76 additions & 0 deletions
76
test/deprecation_toolkit/behaviors/ci_record_helper_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "tempfile" | ||
|
||
module DeprecationToolkit | ||
module Behaviors | ||
class CIRecordHelperTest < ActiveSupport::TestCase | ||
setup do | ||
@tempfolder = Dir.mktmpdir | ||
@logfile_path = "#{@tempfolder}/ci_output.log" | ||
@previous_configuration = Configuration.behavior | ||
@previous_deprecation_path = Configuration.deprecation_path | ||
|
||
Configuration.behavior = CIRecordHelper | ||
Configuration.deprecation_path = @tempfolder | ||
end | ||
|
||
teardown do | ||
Configuration.behavior = @previous_configuration | ||
Configuration.deprecation_path = @previous_deprecation_path | ||
|
||
FileUtils.rm_rf(@tempfolder) | ||
end | ||
|
||
test '#trigger raise an error whom message is parsable by the `deprecation_toolkit:record_from_ci_output` task' do | ||
ci_output = +'' | ||
ci_output << "random stuff generated by CI, this should be filtered out by the Rake Task\n" | ||
|
||
MyTest = Class.new(MiniTest::Test) do | ||
def self.name | ||
'MyTest' | ||
end | ||
|
||
def test_generate_deprecation | ||
ActiveSupport::Deprecation.warn('Foo') | ||
end | ||
|
||
def test_generate_second_deprecation | ||
ActiveSupport::Deprecation.warn('Bar') | ||
end | ||
end | ||
|
||
generate_raw_failure_output(ci_output) | ||
record_deprecation_files | ||
|
||
success_result = MyTest.new('test_generate_deprecation').run | ||
success_result2 = MyTest.new('test_generate_second_deprecation').run | ||
|
||
assert_predicate success_result, :passed? | ||
assert_predicate success_result2, :passed? | ||
end | ||
|
||
private | ||
|
||
def generate_raw_failure_output(ci_output) | ||
failure_result = MyTest.new('test_generate_deprecation').run | ||
failure_result2 = MyTest.new('test_generate_second_deprecation').run | ||
|
||
ci_output << failure_result.failures.first.message << "\n" | ||
ci_output << failure_result2.failures.first.message << "\n" | ||
|
||
File.write(@logfile_path, ci_output) | ||
end | ||
|
||
def record_deprecation_files | ||
ENV["FILEPATH"] = @logfile_path | ||
Rake::Task['deprecation_toolkit:record_from_ci_output'].invoke | ||
|
||
assert File.exist?("#{@tempfolder}/my_test.yml") | ||
ensure | ||
ENV.delete("FILEPATH") | ||
end | ||
end | ||
end | ||
end |