[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / spec / syntax_suggest / spec_helper.rb
blob89bc9f4ab1e638d319fdd8c51ed63547ce2a16ac
1 # frozen_string_literal: true
3 require "bundler/setup"
4 require "syntax_suggest/api"
6 require "benchmark"
7 require "tempfile"
9 RSpec.configure do |config|
10   # Enable flags like --only-failures and --next-failure
11   config.example_status_persistence_file_path = ".rspec_status"
13   # Disable RSpec exposing methods globally on `Module` and `main`
14   config.disable_monkey_patching!
16   config.expect_with :rspec do |c|
17     c.syntax = :expect
18   end
20   if config.color_mode == :automatic
21     if config.color_enabled? && ((ENV["TERM"] == "dumb") || ENV["NO_COLOR"]&.slice(0))
22       config.color_mode = :off
23     end
24   end
25 end
27 # Used for debugging modifications to
28 # display output
29 def debug_display(output)
30   return unless ENV["DEBUG_DISPLAY"]
31   puts
32   puts output
33   puts
34 end
36 def spec_dir
37   Pathname(__dir__)
38 end
40 def lib_dir
41   if ruby_core?
42     root_dir.join("../lib")
43   else
44     root_dir.join("lib")
45   end
46 end
48 def root_dir
49   spec_dir.join("..")
50 end
52 def fixtures_dir
53   spec_dir.join("fixtures")
54 end
56 def ruby_core?
57   !root_dir.join("syntax_suggest.gemspec").exist?
58 end
60 def code_line_array(source)
61   SyntaxSuggest::CleanDocument.new(source: source).call.lines
62 end
64 autoload :RubyProf, "ruby-prof"
66 def debug_perf
67   raise "No block given" unless block_given?
69   if ENV["DEBUG_PERF"]
70     out = nil
71     result = RubyProf.profile do
72       out = yield
73     end
75     dir = SyntaxSuggest.record_dir("tmp")
76     printer = RubyProf::MultiPrinter.new(result, [:flat, :graph, :graph_html, :tree, :call_tree, :stack, :dot])
77     printer.print(path: dir, profile: "profile")
79     out
80   else
81     yield
82   end
83 end
85 def run!(cmd, raise_on_nonzero_exit: true)
86   out = `#{cmd} 2>&1`
87   raise "Command: #{cmd} failed: #{out}" if !$?.success? && raise_on_nonzero_exit
88   out
89 end
91 # Allows us to write cleaner tests since <<~EOM block quotes
92 # strip off all leading indentation and we need it to be preserved
93 # sometimes.
94 class String
95   def indent(number)
96     lines.map do |line|
97       if line.chomp.empty?
98         line
99       else
100         " " * number + line
101       end
102     end.join
103   end