Skip to content

Commit a851193

Browse files
authored
Add Minitest suite for validate_weapons.rb (#36)
Co-authored-by: hahwul <13212227+hahwul@users.noreply.github.com>
1 parent 42afced commit a851193

File tree

1 file changed

+111
-17
lines changed

1 file changed

+111
-17
lines changed

test/test_validate_weapons.rb

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,128 @@
11
require 'minitest/autorun'
2+
require 'tmpdir'
23
require 'fileutils'
4+
require 'yaml'
35
require 'open3'
4-
require 'tmpdir'
56

67
class TestValidateWeapons < Minitest::Test
78
def setup
89
@original_dir = Dir.pwd
9-
@temp_dir = File.join(Dir.tmpdir, "weapons_test_#{Time.now.to_i}")
10-
FileUtils.mkdir_p(File.join(@temp_dir, "weapons"))
10+
@script_path = File.join(@original_dir, 'scripts', 'validate_weapons.rb')
11+
12+
@tmpdir = Dir.mktmpdir
13+
Dir.chdir(@tmpdir)
14+
Dir.mkdir('weapons')
1115
end
1216

1317
def teardown
14-
FileUtils.rm_rf(@temp_dir)
18+
Dir.chdir(@original_dir)
19+
FileUtils.remove_entry(@tmpdir) rescue nil
1520
end
1621

17-
def test_malformed_yaml_is_rescued
18-
# Create a malformed YAML file
19-
File.write(File.join(@temp_dir, "weapons", "malformed.yaml"), ": malformed\nyaml:\n -")
22+
def create_weapon_file(filename, content)
23+
File.write(File.join('weapons', filename), content.to_yaml)
24+
end
25+
26+
def run_script
27+
stdout, stderr, status = Open3.capture3("ruby", @script_path)
28+
output = stdout + stderr
29+
[output, status]
30+
end
31+
32+
def clean_output(output)
33+
output.lines.reject { |l| l.include?("Is a directory") || l.strip.empty? }.join
34+
end
35+
36+
def test_valid_yaml
37+
create_weapon_file('valid.yaml', {
38+
'type' => 'tool',
39+
'lang' => 'ruby',
40+
'url' => 'https://github.com/example/repo',
41+
'tags' => ['security']
42+
})
43+
44+
output, status = run_script
45+
assert status.success?
46+
assert_empty clean_output(output)
47+
end
2048

21-
script_path = File.join(@original_dir, "scripts", "validate_weapons.rb")
49+
def test_empty_type
50+
create_weapon_file('empty_type.yaml', {
51+
'type' => '',
52+
'lang' => 'ruby',
53+
'url' => 'https://github.com/example/repo',
54+
'tags' => ['security']
55+
})
2256

23-
# Run the script in the temporary directory
24-
Dir.chdir(@temp_dir) do
25-
stdout, stderr, status = Open3.capture3("ruby #{script_path}")
57+
output, _ = run_script
58+
assert_includes output, "./weapons/empty_type.yaml :: none-type\n"
59+
end
60+
61+
def test_nil_type
62+
create_weapon_file('nil_type.yaml', {
63+
'type' => nil,
64+
'lang' => 'ruby',
65+
'url' => 'https://github.com/example/repo',
66+
'tags' => ['security']
67+
})
68+
69+
output, _ = run_script
70+
assert_includes output, "./weapons/nil_type.yaml :: none-type\n"
71+
end
72+
73+
def test_empty_lang_with_github_url
74+
create_weapon_file('empty_lang.yaml', {
75+
'type' => 'tool',
76+
'lang' => '',
77+
'url' => 'https://github.com/example/repo',
78+
'tags' => ['security']
79+
})
80+
81+
output, _ = run_script
82+
assert_includes output, "./weapons/empty_lang.yaml :: none-lang\n"
83+
end
84+
85+
def test_nil_lang_with_github_url
86+
create_weapon_file('nil_lang.yaml', {
87+
'type' => 'tool',
88+
'lang' => nil,
89+
'url' => 'https://github.com/example/repo',
90+
'tags' => ['security']
91+
})
92+
93+
output, _ = run_script
94+
assert_includes output, "./weapons/nil_lang.yaml :: none-lang\n"
95+
end
96+
97+
def test_empty_lang_without_github_url
98+
create_weapon_file('empty_lang_no_github.yaml', {
99+
'type' => 'tool',
100+
'lang' => '',
101+
'url' => 'https://example.com/repo',
102+
'tags' => ['security']
103+
})
104+
105+
output, status = run_script
106+
assert status.success?
107+
assert_empty clean_output(output)
108+
end
109+
110+
def test_invalid_yaml_syntax_error
111+
File.write(File.join('weapons', 'invalid.yaml'), "invalid: yaml: content: :")
112+
113+
output, status = run_script
114+
115+
assert status.success?
116+
refute_empty clean_output(output)
117+
assert_match(/Psych::SyntaxError|mapping values are not allowed|did not find expected key/, output)
118+
end
119+
120+
def test_malformed_yaml_is_rescued
121+
File.write(File.join('weapons', 'malformed.yaml'), ": malformed\nyaml:\n -")
26122

27-
# Assert that the error was caught and printed to stdout
28-
assert_match(/did not find expected key/, stdout)
123+
output, status = run_script
29124

30-
# Script should not crash (exit status 0 because it rescued)
31-
assert status.success?
32-
end
125+
assert status.success?
126+
assert_match(/did not find expected key|malformed/, output)
33127
end
34-
end
128+
end

0 commit comments

Comments
 (0)