-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
217 lines (188 loc) · 5.94 KB
/
test.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require "test-unit"
require_relative "exec"
require 'tmpdir'
require 'pathname'
require 'timecop'
require "open3"
class ParseCommandlineArgsTest < Test::Unit::TestCase
data(
"Minimum",
[
["/path/to/file.log"],
["/path/to/file.log", nil, nil, nil, false]
]
)
data(
"Full",
[
["/path/to/file.log", "--hour", "20", "--status-file", "/path/to/status", "--encoding", "shift_jis", "--dry-run"],
["/path/to/file.log", 20, "/path/to/status", "shift_jis", true]
]
)
test "Can parse correct args" do |(args, expected_results)|
results = parse_commandline_args(args)
assert_equal expected_results, results
end
data("No args", [])
data("Invalid hour: not integer", ["/path/to/file.log", "--hour", "not integer"])
data("Invalid hour: wrong integer", ["/path/to/file.log", "--hour", "24"])
data("Invalid encoding", ["/path/to/file.log", "--encoding", "invalid encoding name"])
data("Unassumed args 2", ["/path/to/file.log", "unassumed arg"])
test "Return nil for invalid args" do |args|
results = parse_commandline_args(args)
assert_nil results
end
end
class ReadTest < Test::Unit::TestCase
def setup
Dir.mktmpdir do |tmp_dir|
@tmp_dir = Pathname(tmp_dir)
yield
end
end
def make_testfile(path, content, **args)
File.open(path, "w", **args) do |f|
f.puts content
end
end
def command_to_read_file(path)
if /linux/ === RUBY_PLATFORM or /darwin/ =~ RUBY_PLATFORM
"cat #{path.to_s}"
else
"type #{path.to_s.gsub("/", "\\")}"
end
end
test "read" do
filepath = @tmp_dir + "test"
content = <<~CONTENT
testlog1
testlog2
CONTENT
make_testfile(filepath, content)
result, status = Open3.capture2e("ruby", "exec.rb", command_to_read_file(filepath))
assert_equal 0, status.exitstatus
assert_equal content, result
end
test "Can read shift_jis file with minimum args" do
filepath = @tmp_dir + "test"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content, encoding: "shift_jis")
result, status = Open3.capture2e("ruby", "exec.rb", command_to_read_file(filepath))
assert_equal(
[0, content],
[status.exitstatus, result.encode("utf-8", "shift_jis")]
)
end
test "Can read shift_jis file with encoding option" do
filepath = @tmp_dir + "test"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content, encoding: "shift_jis")
result, status = Open3.capture2e("ruby", "exec.rb", command_to_read_file(filepath), "--encoding", "shift_jis")
assert_equal(
[0, content],
[status.exitstatus, result]
)
end
test "Can read utf-8 file" do
filepath = @tmp_dir + "test"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content, encoding: "utf-8")
result, status = Open3.capture2e("ruby", "exec.rb", command_to_read_file(filepath))
assert_equal(
[0, content],
[status.exitstatus, result]
)
end
test "Can read file with hour" do
filepath = @tmp_dir + "test"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content)
Timecop.freeze(2024, 7, 9, 0, 0, 0) do
result = exec_command(command_to_read_file(filepath), 20, nil, nil, false)
assert_nil result
end
Timecop.freeze(2024, 7, 9, 20, 0, 0) do
result = exec_command(command_to_read_file(filepath), 20, nil, nil, false)
assert_equal content, result
end
end
test "Can read file with status" do
filepath = @tmp_dir + "test"
status_path = @tmp_dir + "status"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content)
Timecop.freeze(2024, 7, 9, 20, 0, 0) do
result = exec_command(command_to_read_file(filepath), 20, status_path.to_s, nil, false)
assert_equal content, result
end
Timecop.freeze(2024, 7, 9, 20, 59, 59) do
result = exec_command(command_to_read_file(filepath), 20, status_path.to_s, nil, false)
assert_nil result
end
Timecop.freeze(2024, 7, 10, 0, 0, 0) do
result = exec_command(command_to_read_file(filepath), 20, status_path.to_s, nil, false)
assert_nil result
end
Timecop.freeze(2024, 7, 10, 20, 0, 0) do
result = exec_command(command_to_read_file(filepath), 20, status_path.to_s, nil, false)
assert_equal content, result
end
end
test "dry-run does not update the status file" do
filepath = @tmp_dir + "test"
status_path = @tmp_dir + "status"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content, encoding: "shift_jis")
result, status = Open3.capture2e("ruby", "exec.rb", command_to_read_file(filepath), "--status-file", status_path.to_s, "--dry-run")
assert_equal(
[0, content, false],
[status.exitstatus, result.encode("utf-8", "shift_jis"), File.exist?(status_path)]
)
end
test "Retry when the command fails" do
filepath = @tmp_dir + "test"
status_path = @tmp_dir + "status"
error_path = @tmp_dir + "error.log"
content = <<~CONTENT
sample log
日本語のログ
CONTENT
make_testfile(filepath, content)
command_to_succeed = "ruby exec.rb \"#{command_to_read_file(filepath)}\" --status-file #{status_path.to_s} 2> #{error_path.to_s}"
command_to_fail = "ruby exec.rb \"#{command_to_read_file(filepath + 'wrong-path')}\" --status-file #{status_path.to_s} 2> #{error_path.to_s}"
result, status = Open3.capture2e(command_to_fail)
assert_equal(
["", 0],
[result, status.exitstatus]
)
assert do
not File.read(error_path).empty?
end
result, status = Open3.capture2e(command_to_succeed)
assert_equal(
[content, 0],
[result, status.exitstatus]
)
assert do
File.read(error_path).empty?
end
end
end