Kouhei Sutou
null+****@clear*****
Tue Apr 26 14:54:21 JST 2016
Kouhei Sutou 2016-04-26 14:54:21 +0900 (Tue, 26 Apr 2016) New Revision: c6f1d3a03ac993cba7f5fe5e45aebd611f8fe8f8 https://github.com/groonga/grntest/commit/c6f1d3a03ac993cba7f5fe5e45aebd611f8fe8f8 Message: Implement internal diff reporter It's for environment that doesn't have diff command such as Windows. Added files: lib/grntest/diff-reporter.rb Modified files: grntest.gemspec lib/grntest/reporters/base-reporter.rb lib/grntest/tester.rb Modified: grntest.gemspec (+1 -0) =================================================================== --- grntest.gemspec 2016-04-26 14:14:15 +0900 (c2e97b7) +++ grntest.gemspec 2016-04-26 14:54:21 +0900 (da738cb) @@ -45,6 +45,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency("json") spec.add_runtime_dependency("msgpack") + spec.add_runtime_dependency("lcs-diff") spec.add_runtime_dependency("groonga-command-parser") spec.add_development_dependency("bundler") Added: lib/grntest/diff-reporter.rb (+78 -0) 100644 =================================================================== --- /dev/null +++ lib/grntest/diff-reporter.rb 2016-04-26 14:54:21 +0900 (799e816) @@ -0,0 +1,78 @@ +# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +require "diff/lcs" +require "diff/lcs/hunk" + +module Grntest + class DiffReporter + def initialize(expected, actual) + @expected = expected + @actual = actual + end + + def report + expected_lines =****@expec*****(&:chomp) + actual_lines =****@actua*****(&:chomp) + diffs = Diff::LCS.diff(expected_lines, + actual_lines) + return if diffs.empty? + + report_expected_label("(expected)") + report_actual_label("(actual)") + + context_n_lines = 3 + previous_hunk = nil + file_length_diff = 0 + diffs.each do |diff| + begin + hunk = Diff::LCS::Hunk.new(expected_lines, + actual_lines, + diff, + context_n_lines, + file_length_diff) + next if previous_hunk.nil? + next if hunk.merge(previous_hunk) + + report_hunk(previous_hunk) + ensure + previous_hunk = hunk + end + end + report_hunk(previous_hunk) + end + + private + def expected_mark + "-" + end + + def actual_mark + "+" + end + + def report_expected_label(content) + puts("#{expected_mark * 3} #{content}") + end + + def report_actual_label(content) + puts("#{actual_mark * 3} #{content}") + end + + def report_hunk(hunk) + puts(hunk.diff(:unified)) + end + end +end Modified: lib/grntest/reporters/base-reporter.rb (+20 -3) =================================================================== --- lib/grntest/reporters/base-reporter.rb 2016-04-26 14:14:15 +0900 (8faa3d3) +++ lib/grntest/reporters/base-reporter.rb 2016-04-26 14:54:21 +0900 (33a11a0) @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2015 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2016 Kouhei Sutou <kou �� clear-code.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +require "grntest/diff-reporter" + module Grntest module Reporters class BaseReporter @@ -93,16 +95,31 @@ module Grntest end def report_diff(expected, actual) + if****@teste***** == "internal" + run_diff_reporter(expected, actual) + else + run_diff_command(expected, actual) + end + end + + def run_diff_command(expected, actual) create_temporary_file("expected", expected) do |expected_file| create_temporary_file("actual", actual) do |actual_file| diff_options =****@teste*****_options.dup - diff_options.concat(["--label", "(expected)", expected_file.path, - "--label", "(actual)", actual_file.path]) + diff_options += [ + "--label", "(expected)", expected_file.path, + "--label", "(actual)", actual_file.path, + ] system(@tester.diff, *diff_options) end end end + def run_diff_reporter(expected, actual) + reporter = DiffReporter.new(expected, actual) + reporter.report + end + def report_test(worker, result) report_marker(result) print("[#{worker.id}] ") if****@teste*****_workers > 1 Modified: lib/grntest/tester.rb (+14 -3) =================================================================== --- lib/grntest/tester.rb 2016-04-26 14:14:15 +0900 (173f0a8) +++ lib/grntest/tester.rb 2016-04-26 14:54:21 +0900 (75fe11a) @@ -101,6 +101,7 @@ module Grntest parser.on("--diff=DIFF", "Use DIFF as diff command", + "Use --diff=internal to use internal differ", "(#{tester.diff})") do |diff| tester.diff = diff tester.diff_options.clear @@ -391,9 +392,12 @@ module Grntest if command_exist?("cut-diff") @diff = "cut-diff" @diff_options = ["--context-lines", "10"] - else + elsif command_exist?("diff") @diff = "diff" @diff_options = ["-u"] + else + @diff = "internal" + @diff_options = [] end end @@ -409,9 +413,16 @@ module Grntest end def command_exist?(name) + exeext = RbConfig::CONFIG["EXEEXT"] ENV["PATH"].split(File::PATH_SEPARATOR).each do |path| - absolute_path = File.join(path, name) - return true if File.executable?(absolute_path) + raw_candidate = File.join(path, name) + candidates = [ + raw_candidate, + "#{raw_candidate}#{exeext}", + ] + candidates.each do |candidate| + return true if File.executable?(candidate) + end end false end -------------- next part -------------- HTML����������������������������... Télécharger