Module: DiffTest::Helper

Defined in:
lib/diff_test/helper.rb

Constant Summary collapse

RELATIVE_PATH_FROM_PROJECT_ROOT_CACHE =
{}

Class Method Summary collapse

Class Method Details

.absolute_path_from_project_root(file_path) ⇒ Object



28
29
30
# File 'lib/diff_test/helper.rb', line 28

def absolute_path_from_project_root(file_path)
  File.join(DiffTest.configuration.project_root, file_path)
end

.bm(*name, &block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/diff_test/helper.rb', line 70

def bm(*name, &block)
  puts "Starting #{name.join(' ')}------------"
  start = Time.now
  result = yield
  puts "Finished #{name.join(' ')} in #{(Time.now - start).round(2)}s------------"
  result
end

.branch_nameObject



62
63
64
# File 'lib/diff_test/helper.rb', line 62

def branch_name
  @branch_name ||= `git rev-parse --abbrev-ref HEAD`.strip
end

.commit_titleObject



58
59
60
# File 'lib/diff_test/helper.rb', line 58

def commit_title
  @commit_title ||= `git log -1 --pretty=%B`.strip
end

.committer_emailObject



66
67
68
# File 'lib/diff_test/helper.rb', line 66

def committer_email
  @committer_email ||= `git log -1 --pretty=%ae`.strip
end

.const_source_path(const_name) ⇒ Object



24
25
26
# File 'lib/diff_test/helper.rb', line 24

def const_source_path(const_name)
  Module.const_source_location(const_name).first
end

.expand_path(file_path) ⇒ Object



38
39
40
# File 'lib/diff_test/helper.rb', line 38

def expand_path(file_path)
  File.expand_path(file_path, DiffTest.configuration.project_root)
end

.file_in_project?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/diff_test/helper.rb', line 32

def file_in_project?(file_path)
  file_path &&
    file_path.start_with?(DiffTest.configuration.project_root) &&
    !file_path.start_with?(DiffTest.configuration.vendor_bundle_path)
end

.log(message) ⇒ Object



78
79
80
# File 'lib/diff_test/helper.rb', line 78

def log(message)
  puts "DiffTest #{Time.current.strftime('%H:%M:%S')}: #{message}" if ENV['DIFF_TEST_LOG']
end

.on_main_branch?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/diff_test/helper.rb', line 42

def on_main_branch?
  branch_name == 'master' || branch_name == 'main'
end

.on_skipped_branch?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/diff_test/helper.rb', line 46

def on_skipped_branch?
  branch_name.match?(/skip[\s\-_]*diff[\s\-_]*test/i)
end

.on_skipped_commit?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/diff_test/helper.rb', line 50

def on_skipped_commit?
  commit_title.match?(/skip[\s\-_]*diff[\s\-_]*test/i)
end

.relative_const_source_path_from_project_root(const_name) ⇒ Object



20
21
22
# File 'lib/diff_test/helper.rb', line 20

def relative_const_source_path_from_project_root(const_name)
  relative_path_from_project_root(const_source_path(const_name))
end

.relative_path_from_project_root(file_path) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/diff_test/helper.rb', line 11

def relative_path_from_project_root(file_path)
  value = RELATIVE_PATH_FROM_PROJECT_ROOT_CACHE[file_path]
  return value unless value.nil?

  raise 'Expected file_path to be an absolute path' unless file_path.start_with?('/')

  RELATIVE_PATH_FROM_PROJECT_ROOT_CACHE[file_path] = Pathname.new(file_path).relative_path_from(DiffTest.configuration.project_root).to_s.freeze
end

.rev_shaObject



54
55
56
# File 'lib/diff_test/helper.rb', line 54

def rev_sha
  @rev_sha ||= `git rev-parse HEAD`.strip
end

.test_id(test_file_path, test_name) ⇒ Object



7
8
9
# File 'lib/diff_test/helper.rb', line 7

def test_id(test_file_path, test_name)
  [test_file_path, test_name].join(':')
end