Skip to content

Commit 4892a6b

Browse files
authored
Add test for yaml error rescue in erb.rb (#38)
Co-authored-by: hahwul <13212227+hahwul@users.noreply.github.com>
1 parent 7cc223c commit 4892a6b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/erb_test.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'minitest/autorun'
2+
require 'fileutils'
3+
require 'open3'
4+
require 'tmpdir'
5+
6+
class TestErbScript < Minitest::Test
7+
def setup
8+
# Save current directory
9+
@original_dir = Dir.pwd
10+
11+
# Create a temporary directory for testing
12+
@temp_dir = Dir.mktmpdir("weapons_test")
13+
14+
# Change to temp directory
15+
Dir.chdir(@temp_dir)
16+
17+
# Set up necessary directory structure
18+
FileUtils.mkdir_p('weapons')
19+
FileUtils.mkdir_p('images')
20+
FileUtils.mkdir_p('categorize/tags')
21+
FileUtils.mkdir_p('categorize/langs')
22+
23+
# Create required files that erb.rb expects
24+
FileUtils.touch('images/mhw-dark.png')
25+
FileUtils.touch('images/mhw-light.png')
26+
FileUtils.touch('CONTRIBUTING.md')
27+
end
28+
29+
def teardown
30+
# Change back to original directory
31+
Dir.chdir(@original_dir)
32+
33+
# Remove the temporary directory
34+
FileUtils.remove_entry(@temp_dir)
35+
end
36+
37+
def test_yaml_load_error_handling
38+
# Create an invalid YAML file in the temp weapons directory
39+
File.write('weapons/invalid.yaml', "invalid: \n- : : ")
40+
41+
# Create a valid YAML file to ensure the script continues processing
42+
valid_yaml = <<~YAML
43+
name: TestTool
44+
type: Pentest
45+
description: A test tool
46+
platform: ['linux']
47+
tags: ['test']
48+
lang: Ruby
49+
url: https://github.com/test/tool
50+
category: All
51+
YAML
52+
File.write('weapons/valid.yaml', valid_yaml)
53+
54+
# Run the erb.rb script from the original directory inside the temp directory context
55+
script_path = File.join(@original_dir, 'scripts/erb.rb')
56+
stdout, stderr, status = Open3.capture3("ruby #{script_path}")
57+
58+
# The script should exit successfully because the error is rescued
59+
assert status.success?, "Script failed to execute successfully. Stderr: #{stderr}"
60+
61+
# The output should contain the syntax error from the invalid YAML
62+
assert_match(/did not find expected key|Psych::SyntaxError/, stdout)
63+
64+
# The script should still process the valid YAML and generate output
65+
assert File.exist?('README.md'), "README.md was not generated"
66+
readme_content = File.read('README.md')
67+
assert_match(/TestTool/, readme_content)
68+
69+
# Verify categorized files were also generated
70+
assert File.exist?('categorize/tags/test.md'), "Tag markdown was not generated"
71+
assert File.exist?('categorize/langs/Ruby.md'), "Lang markdown was not generated"
72+
end
73+
end

0 commit comments

Comments
 (0)