File: Rakefile

package info (click to toggle)
ruby-maxitest 6.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 504 kB
  • sloc: ruby: 1,583; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 3,632 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "bundler/setup"
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

begin
  require "bump/tasks"
  Bump.replace_in_default = Dir["gemfiles/**.lock"]
rescue LoadError # not available in gemfiles/
end

module VendorUpdate
  class << self
    def all
      require "open-uri"
      Dir["lib/maxitest/vendor/*"].each { |f| update_file f }
    end

    private

    def update_file(file)
      # a file has multiple parts, and each part starts with the url where it was copied from
      do_not_modify = "generated by rake update, do not modify"
      start = "# BEGIN #{do_not_modify}\n"
      finish = "# END #{do_not_modify}\n"
      urls = File.read(file).scan(/#{Regexp.escape start}# (\S+)\n.*?#{Regexp.escape(finish)}/m).flatten(1)

      urls.map! do |url|
        code = URI.open(url).read
        code = modify_code code, url
        "#{start}# #{url}\n#{code}\n#{finish}"
      end

      File.write(file, urls.reject(&:empty?).join("\n"))
    end

    def modify_code(code, url)
      code = code.dup
      code.gsub!(/require .*?\n/, "") # we inline everything
      code.strip!
      code = "=begin\n#{code}\n=end" if url.include?("LICENSE")

      if url.end_with?("/testrbl.rb")
        # nest under Maxitest to avoid collision, more modifications are done in bin/mtest
        code = "module Maxitest\n#{code.gsub(/^/, "  ").gsub(/^\s+$/, "")}\nend"
      elsif url.end_with?("/line_plugin.rb")
        # add rails detector
        raise unless code.sub!(%{pwd = Pathname.new(Dir.pwd)}, %{pwd = Pathname.new(Dir.pwd)\n      bin_rails = File.exist?("bin/rails")})
        # replace ruby with `mtest` or `bin/rails test`
        # to work around https://github.com/minitest/minitest-rails/issues/256
        raise unless code.sub!(%{output = "ruby \#{file} -l \#{line}"}, %{output = "\#{bin_rails ? "bin/rails test" : "mtest"} \#{file}:\#{line}"})
      elsif url.end_with?('/around/spec.rb')
        # do not fail with resume for nil class when before was never called
        # for example when putting <% raise %> into a fixture file
        raise unless code.sub!(%{fib.resume unless fib == :failed}, %{fib.resume if fib && fib != :failed})

        # make `after :all` blow up to avoid confusion
        raise unless code.sub!(%{fib = nil}, %{raise ArgumentError, "only :each or no argument is supported" if args != [] && args != [:each]\n    fib = nil})
      elsif url.end_with?('/rg_plugin.rb')
        # support disabling/enabling colors
        # https://github.com/blowmage/minitest-rg/pull/15
        raise unless code.sub!(
          %(opts.on "--rg", "Add red/green to test output." do\n      RG.rg!),
          %(opts.on "--[no-]rg", "Add red/green to test output." do |bool|\n      RG.rg! bool),
        )
        raise unless code.sub!(
          %(    def self.rg!\n      @rg = true),
          %(    def self.rg!(bool = true)\n      @rg = bool),
        )
        raise unless code.sub!(
          "reporter.reporters.grep(Minitest::Reporter).each do |rep|\n        rep.io = io if rep.io.tty?",
          "reporter.reporters.grep(Minitest::Reporter).each do |rep|\n        rep.io = io"
        )
        raise unless code.sub!(
          "MiniTest",
          "Minitest",
        )
      end
      code
    end
  end
end

desc "Run all tests"
task default: :spec

desc "Update all dependencies"
task :update do
  VendorUpdate.all
end

desc "bundle all gemfiles/ EXTRA="
task :bundle do
  extra = ENV["EXTRA"]
  Bundler.with_original_env do
    Dir["gemfiles/*.gemfile"].reverse.each { |gemfile| sh "BUNDLE_GEMFILE=#{gemfile} bundle #{extra}" }
  end
end