minitest-heat 0.0.10 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,118 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- module Minitest
6
- module Heat
7
- class Backtrace
8
- # Represents a line from a backtrace to provide more convenient access to information about
9
- # the relevant file and line number for displaying in test results
10
- class Line
11
- attr_accessor :pathname, :number, :container
12
- alias line_number number
13
-
14
- # Creates an instance of a line number reference
15
- # @param pathname: [Pathname, String] the full pathname to the file
16
- # @param number: [Integer, String] the line number in question
17
- # @param container: nil [String] the containing method or block for the line of code
18
- #
19
- # @return [self]
20
- def initialize(pathname:, number:, container: nil)
21
- @pathname = Pathname(pathname)
22
- @number = number.to_i
23
- @container = container.to_s
24
- end
25
-
26
- # Parses a line from a backtrace in order to convert it to usable components
27
- def self.parse_backtrace(raw_text)
28
- raw_pathname, raw_line_number, raw_container = raw_text.split(':')
29
- raw_container = raw_container.delete_prefix('in `').delete_suffix("'")
30
-
31
- new(pathname: raw_pathname, number: raw_line_number, container: raw_container)
32
- end
33
-
34
- # Generates a formatted string describing the line of code similar to the original backtrace
35
- #
36
- # @return [String] a consistently-formatted, human-readable string about the line of code
37
- def to_s
38
- "#{location} in `#{container}`"
39
- end
40
-
41
- # A safe interface to getting a string representing the path portion of the file
42
- #
43
- # @return [String] either the path/directory portion of the file name or '(Unrecognized File)'
44
- # if the offending file can't be found for some reason
45
- def path
46
- pathname.exist? ? pathname.dirname : UNRECOGNIZED
47
- end
48
-
49
- # A safe interface for getting a string representing the filename portion of the file
50
- #
51
- # @return [String] either the filename portion of the file or '(Unrecognized File)'
52
- # if the offending file can't be found for some reason
53
- def file
54
- pathname.exist? ? pathname.basename : UNRECOGNIZED
55
- end
56
-
57
- # A safe interface to getting the last modified time for the file in question
58
- #
59
- # @return [Time] the timestamp for when the file in question was last modified or `Time.at(0)`
60
- # if the offending file can't be found for some reason
61
- def mtime
62
- pathname.exist? ? pathname.mtime : UNKNOWN_MODIFICATION_TIME
63
- end
64
-
65
- # A safe interface to getting the number of seconds since the file was modified
66
- #
67
- # @return [Integer] the number of seconds since the file was modified or `-1` if the offending
68
- # file can't be found for some reason
69
- def age_in_seconds
70
- pathname.exist? ? seconds_ago : UNKNOWN_MODIFICATION_SECONDS
71
- end
72
-
73
- # A convenient method for getting the full location identifier using the full pathname and
74
- # line number separated by a `:`
75
- #
76
- # @return [String] the full pathname and line number
77
- def location
78
- "#{pathname}:#{number}"
79
- end
80
-
81
- # A convenient method for getting the short location with `Dir.pwd` removed
82
- #
83
- # @return [String] the relative pathname and line number
84
- def short_location
85
- "#{file}:#{number}"
86
- end
87
-
88
- # A convenient method for getting the line of source code for the offending line number
89
- #
90
- # @return [String] the source code for the file/line number combination
91
- def source_code(max_line_count: 1)
92
- Minitest::Heat::Source.new(
93
- pathname.to_s,
94
- line_number: line_number,
95
- max_line_count: max_line_count
96
- )
97
- end
98
-
99
- # Determines if a given file follows the standard approaching to naming test files.
100
- #
101
- # @return [Boolean] true if the file name starts with `test_` or ends with `_test.rb`
102
- def test_file?
103
- file.to_s.start_with?('test_') || file.to_s.end_with?('_test.rb')
104
- end
105
-
106
- private
107
-
108
- UNRECOGNIZED = '(Unrecognized File)'
109
- UNKNOWN_MODIFICATION_TIME = Time.at(0)
110
- UNKNOWN_MODIFICATION_SECONDS = -1
111
-
112
- def seconds_ago
113
- (Time.now - mtime).to_i
114
- end
115
- end
116
- end
117
- end
118
- end