-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
helper.rb
151 lines (124 loc) · 5.46 KB
/
helper.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true
if ENV["TRAVIS"] == "true" && ENV["CODE_COVERAGE"] == "true"
require "simplecov"
require "codecov"
SimpleCov.formatter = SimpleCov::Formatter::Codecov
SimpleCov.start do
add_filter "/test/"
end
end
ENV["MT_HELL"] = "1" # Forces tests to have at least one assertion to pass.
require "minitest/autorun"
# Runs tests in parallel. Also, in combination with the MT_HELL environment var
# set above and the loading of the `minitest-proveit` gem, requires that tests
# have at least one assertion to pass.
require "minitest/hell"
require "minitest/pride"
require "open3"
require "securerandom"
require "friends"
CONTENT = <<-FILE.freeze
### Activities:
- 2018-02-06: @science:indoors:agronomy-with-hydroponics: **Norman Borlaug** and **George Washington Carver** scored a tour of _Atlantis_' hydroponics gardens through wetplants@example.org and they took me along.
- 2015-11-01: **Grace Hopper** and I went to _Marie's Diner_. George had to cancel at the last minute. @food
- 2015-01-04: Got lunch with **Grace Hopper** and **George Washington Carver**. @food
- 2014-12-31: Celebrated the new year in _Paris_ with **Marie Curie**. @partying
- 2014-11-15: Talked to **George Washington Carver** on the phone for an hour.
### Notes:
- 2017-03-12: **Marie Curie** completed her PhD in record time. @school
- 2015-06-15: **Grace Hopper** found out she's getting a big Naval Academy building named after her. @navy
- 2015-06-06: **Marie Curie** just got accepted into a PhD program in _Paris_. @school
- 2015-01-04: **Grace Hopper** and **George Washington Carver** both won an award.
### Friends:
- George Washington Carver
- Grace Hopper (a.k.a. The Admiral a.k.a. Amazing Grace) [Paris] @navy @science
- Marie Curie [Atlantis] @science
- Norman Borlaug (a.k.a. Norm) @science @science:outdoors @science:outdoors:agronomy
- Stanislav Petrov (a.k.a. Stan) @doesnt-trust-computers @doesnt-trust-computers:military-uses
### Locations:
- Atlantis
- Marie's Diner
- Paris
FILE
# This is CONTENT but with activities, friends, and locations unsorted.
SCRAMBLED_CONTENT = <<-FILE.freeze
### Activities:
- 2015-01-04: Got lunch with **Grace Hopper** and **George Washington Carver**. @food
- 2015-11-01: **Grace Hopper** and I went to _Marie's Diner_. George had to cancel at the last minute. @food
- 2018-02-06: @science:indoors:agronomy-with-hydroponics: **Norman Borlaug** and **George Washington Carver** scored a tour of _Atlantis_' hydroponics gardens through wetplants@example.org and they took me along.
- 2014-11-15: Talked to **George Washington Carver** on the phone for an hour.
- 2014-12-31: Celebrated the new year in _Paris_ with **Marie Curie**. @partying
### Notes:
- 2015-01-04: **Grace Hopper** and **George Washington Carver** both won an award.
- 2015-06-06: **Marie Curie** just got accepted into a PhD program in _Paris_. @school
- 2017-03-12: **Marie Curie** completed her PhD in record time. @school
- 2015-06-15: **Grace Hopper** found out she's getting a big Naval Academy building named after her. @navy
### Friends:
- George Washington Carver
- Marie Curie [Atlantis] @science
- Grace Hopper (a.k.a. The Admiral a.k.a. Amazing Grace) [Paris] @navy @science
- Stanislav Petrov (a.k.a. Stan) @doesnt-trust-computers @doesnt-trust-computers:military-uses
- Norman Borlaug (a.k.a. Norm) @science @science:outdoors @science:outdoors:agronomy
### Locations:
- Paris
- Atlantis
- Marie's Diner
FILE
# Define these methods so they can be referenced in the methods below. They'll be overridden in
# test files.
def filename; end
def subject; end
def run_cmd(command, env_vars: "", **args)
stdout, stderr, status = Open3.capture3(
"#{env_vars} bundle exec bin/friends --colorless --filename #{filename} #{command}",
**args
)
{
stdout: stdout,
stderr: stderr,
status: status.exitstatus
}
end
# @param str [String] a string
# @return [String] the input string with a newline appended to it if one was not already
# present, *unless* the string is empty
def ensure_trailing_newline_unless_empty(str)
return "" if str.empty?
str.to_s[-1] == "\n" ? str.to_s : "#{str}\n"
end
def stdout_only(expected)
puts subject[:stderr] unless subject[:stderr] == ""
value(subject[:stdout]).must_equal ensure_trailing_newline_unless_empty(expected)
value(subject[:stderr]).must_equal ""
value(subject[:status]).must_equal 0
end
def stderr_only(expected)
value(subject[:stdout]).must_equal ""
value(subject[:stderr]).must_equal ensure_trailing_newline_unless_empty(expected)
value(subject[:status]).must_be :>, 0
end
def file_equals(expected)
subject
value(File.read(filename)).must_equal expected
end
def line_changed(expected_old, expected_new)
index = File.read(filename).split("\n").index(expected_old)
value(index).must_be_kind_of Numeric # Not nil, so we know that `expected_old` was found.
subject
value(File.read(filename).split("\n")[index]).must_equal expected_new
end
def line_added(expected)
n_initial_lines = File.read(filename).split("\n").size
subject
lines = File.read(filename).split("\n")
value(lines.index(expected)).must_be_kind_of Numeric # Not nil, so we know `expected` was found.
value(lines.size).must_equal(n_initial_lines + 1) # Line was added, not changed.
end
def clean_describe(desc, &block)
describe desc do
let(:filename) { "test/tmp/friends#{SecureRandom.uuid}.md" }
before { File.write(filename, content) unless content.nil? }
after { File.delete(filename) if File.exist?(filename) }
class_eval(&block)
end
end