Skip to content

Commit 7cc223c

Browse files
authored
Add error handling test for erb.rb data processing (#39)
Adds a minitest script `test/test_erb.rb` to ensure that malformed weapon data encountered during the categorization logic doesn't crash `scripts/erb.rb`. The test creates a sandbox using `mktmpdir` to prevent repository pollution, simulates a bad YAML file, and asserts the process exits 0 after catching the exception. Co-authored-by: hahwul <13212227+hahwul@users.noreply.github.com>
1 parent 932aeb8 commit 7cc223c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/test_erb.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'minitest/autorun'
2+
require 'open3'
3+
require 'tmpdir'
4+
require 'fileutils'
5+
6+
class TestErbScript < Minitest::Test
7+
def setup
8+
@original_dir = Dir.pwd
9+
@tmp_dir = Dir.mktmpdir
10+
11+
# Copy necessary directories to the temp dir to avoid modifying the real repo
12+
%w[scripts images weapons categorize].each do |dir|
13+
FileUtils.cp_r(File.join(@original_dir, dir), @tmp_dir)
14+
end
15+
16+
# Add dummy file to satisfy README.md template (images/mhw-dark.png, etc)
17+
# The real files were copied, so we are good.
18+
19+
# Write a malformed YAML file that triggers an error in the second data processing block
20+
@test_file = File.join(@tmp_dir, "weapons", "zz_malformed_test.yml")
21+
File.write(@test_file, <<~YAML)
22+
name: malformed
23+
category: android
24+
description: test
25+
url: ''
26+
platform: []
27+
tags: 123
28+
lang: []
29+
type: none
30+
YAML
31+
end
32+
33+
def teardown
34+
FileUtils.remove_entry(@tmp_dir)
35+
end
36+
37+
def test_error_handling_in_data_processing
38+
# Change directory to the temp dir and run the script
39+
Dir.chdir(@tmp_dir) do
40+
# Run the script and capture output
41+
stdout, stderr, status = Open3.capture3("ruby scripts/erb.rb")
42+
43+
# Script should rescue the error and not crash
44+
assert_equal 0, status.exitstatus, "Script should not crash on malformed data"
45+
46+
# The output should contain the specific error message caught by rescue => e
47+
assert_match /undefined method `each' for 123:Integer/m, stdout, "Should catch and print error from processing malformed tags"
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)