-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-code-packager.sh
executable file
·209 lines (183 loc) · 7.33 KB
/
test-code-packager.sh
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
#!/bin/bash
# Test script for code-packager
# Function to run the code-packager with given options and compare the output
run_test() {
local test_name="$1"
local options="$2"
local expected_output_pattern="$3"
echo "Running test: $test_name"
output=$(./code-packager $options 2>&1)
if [[ "$output" =~ $expected_output_pattern ]]; then
echo "Test passed: $test_name"
else
echo "Test failed: $test_name"
echo "Expected output pattern:"
echo "$expected_output_pattern"
echo "Actual output:"
echo "$output"
fi
echo "------------------------"
}
# Function to validate JSON structure
validate_json() {
local json_file="$1"
if ! jq empty "$json_file" > /dev/null; then
echo "Error: Invalid JSON structure in '$json_file'."
exit 1
fi
}
# Get the current directory
current_dir=$(pwd)
# Test case 1: Including multiple file types
test_include_multiple_types() {
local options="-t $current_dir -o code.json -i .sh -i .md -s 2048 -z 1"
local expected_output_pattern=$'Output file zipped: code.zip\nJSON output saved to: code.json\nDirectory tree:'
run_test "Including multiple file types" "$options" "$expected_output_pattern"
validate_json "code.json"
}
# Test case 2: Excluding specific file types (without inclusion)
test_exclude_specific_types() {
local options="-t $current_dir -o code.json -e .txt -e .md -d 1"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Excluding specific file types (without inclusion)" "$options" "$expected_output_pattern"
validate_json "code.json"
}
# Test case 3: Packaging all file types
test_package_all_types() {
local options="-t $current_dir -o code.json -s 10240 -g 0"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Packaging all file types" "$options" "$expected_output_pattern"
validate_json "code.json"
}
# Test case 4: Missing required parameters
test_missing_parameters() {
local options=""
local expected_output_pattern=$'Directory path is required.\nUsage: ./code-packager -t <directory_path> -o <output_file> \\[options\\]'
run_test "Missing required parameters" "$options" "$expected_output_pattern"
}
# Test case 5: Displaying version information
test_version_info() {
local options="-v"
local expected_output_pattern="Code Packager for Language Models - Version"
run_test "Displaying version information" "$options" "$expected_output_pattern"
}
# Test case 6: Displaying help information
test_help_info() {
local options="-h"
local expected_output_pattern="Usage: ./code-packager -t <directory_path> -o <output_file> \\[options\\]"
run_test "Displaying help information" "$options" "$expected_output_pattern"
}
# Test case 7: Empty directory
test_empty_dir() {
local temp_dir=$(mktemp -d)
local options="-t $temp_dir -o empty_dir.json"
local expected_output_pattern=$'JSON output saved to: empty_dir.json\nDirectory tree:'
run_test "Empty directory" "$options" "$expected_output_pattern"
validate_json "empty_dir.json"
rm -rf "$temp_dir" # Cleanup
}
# Test case 8: Respecting .gitignore
test_gitignore() {
local temp_dir=$(mktemp -d)
touch "$temp_dir/ignored.txt"
echo "ignored.txt" > "$temp_dir/.gitignore"
local options="-t $temp_dir -o gitignore_test.json"
local expected_output_pattern=$'JSON output saved to: gitignore_test.json\nDirectory tree:'
run_test "Respecting .gitignore" "$options" "$expected_output_pattern"
validate_json "gitignore_test.json"
rm -rf "$temp_dir" # Cleanup
}
# Test case 9: Invalid input option
test_invalid_option() {
local options="-t . -o output.json -x invalid_option"
local expected_output_pattern="^.*: illegal option -- x"
run_test "Invalid input option" "$options" "$expected_output_pattern"
}
# Test case 10: Output directory specified
test_output_directory() {
local temp_dir=$(mktemp -d)
local options="-t $current_dir -o $temp_dir"
local expected_output_pattern=$'JSON output saved to: '"$temp_dir/$(basename "$current_dir").json"$'\nDirectory tree:'
run_test "Output directory specified" "$options" "$expected_output_pattern"
validate_json "$temp_dir/$(basename "$current_dir").json"
rm -rf "$temp_dir" # Cleanup
}
# Test case 11: Limiting search depth
test_max_depth() {
local options="-t $current_dir -o code.json -m 1"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Limiting search depth" "$options" "$expected_output_pattern"
validate_json "code.json"
}
# Test case 12: Including specific filenames
test_include_specific_filenames() {
local temp_dir=$(mktemp -d)
touch "$temp_dir/README.md" "$temp_dir/LICENSE"
local options="-t $temp_dir -o code.json -I README.md -I LICENSE"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Including specific filenames" "$options" "$expected_output_pattern"
validate_json "code.json"
rm -rf "$temp_dir"
}
# Test case 13: Excluding specific filenames
test_exclude_specific_filenames() {
local temp_dir=$(mktemp -d)
touch "$temp_dir/README.md" "$temp_dir/LICENSE" "$temp_dir/test.txt"
local options="-t $temp_dir -o code.json -E README.md -E LICENSE"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Excluding specific filenames" "$options" "$expected_output_pattern"
validate_json "code.json"
rm -rf "$temp_dir"
}
# Test case 14: Combining extensions and filenames
test_combine_extensions_and_filenames() {
local temp_dir=$(mktemp -d)
touch "$temp_dir/test.py" "$temp_dir/README.md" "$temp_dir/LICENSE"
local options="-t $temp_dir -o code.json -i .py -I README.md -E LICENSE"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Combining extensions and filenames" "$options" "$expected_output_pattern"
validate_json "code.json"
rm -rf "$temp_dir"
}
# Test case 15: Case sensitivity test
test_case_sensitivity() {
local temp_dir=$(mktemp -d)
touch "$temp_dir/README.md" "$temp_dir/readme.md"
local options="-t $temp_dir -o code.json -I README.md"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Case sensitivity" "$options" "$expected_output_pattern"
validate_json "code.json"
rm -rf "$temp_dir"
}
# Test case 16: Non-existent filename
test_nonexistent_filename() {
local temp_dir=$(mktemp -d)
local options="-t $temp_dir -o code.json -I NONEXISTENT"
local expected_output_pattern=$'JSON output saved to: code.json\nDirectory tree:'
run_test "Non-existent filename" "$options" "$expected_output_pattern"
validate_json "code.json"
rm -rf "$temp_dir"
}
# Cleanup function to remove generated files
cleanup() {
rm -f code.json code.zip empty_dir.json gitignore_test.json output.json
}
# Run all test cases
test_include_multiple_types
test_exclude_specific_types
test_package_all_types
test_missing_parameters
test_version_info
test_help_info
test_empty_dir
test_gitignore
test_invalid_option
test_output_directory
test_max_depth
test_include_specific_filenames
test_exclude_specific_filenames
test_combine_extensions_and_filenames
test_case_sensitivity
test_nonexistent_filename
# Cleanup generated files
cleanup