Skip to content

Commit 101577c

Browse files
committed
Add tests (round 1)
1 parent 882a6f3 commit 101577c

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed

test/fixtures/sample.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This is a sample JavaScript file
2+
// Used for testing the context script
3+
// It has multiple lines of comments
4+
5+
function testFunction() {
6+
console.log("Hello, world!");
7+
return true;
8+
}
9+
10+
module.exports = {
11+
testFunction
12+
};

test/test_runner.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# test_runner.sh - Main test runner for context script
4+
5+
# Exit on error
6+
set -e
7+
8+
# Get the directory where the test_runner.sh script is located
9+
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
11+
12+
# Source test utilities
13+
source "$TEST_DIR/test_utils.sh"
14+
15+
# Print header
16+
echo "===================================="
17+
echo "Running tests for context script"
18+
echo "===================================="
19+
20+
# Run all test files
21+
for test_file in "$TEST_DIR"/unit/test_*.sh; do
22+
if [ -f "$test_file" ]; then
23+
echo ""
24+
echo "Running tests in $(basename "$test_file")"
25+
echo "------------------------------------"
26+
bash "$test_file"
27+
fi
28+
done
29+
30+
# Print summary
31+
echo ""
32+
echo "===================================="
33+
echo "Test Results: $TESTS_PASSED passed, $TESTS_FAILED failed"
34+
echo "===================================="
35+
36+
# Return non-zero exit code if any tests failed
37+
if [ $TESTS_FAILED -gt 0 ]; then
38+
exit 1
39+
fi
40+
41+
exit 0

test/test_utils.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
3+
# test_utils.sh - Utilities for testing the context script
4+
5+
# Initialize test counters
6+
TESTS_PASSED=0
7+
TESTS_FAILED=0
8+
9+
# Get the directory where the test_utils.sh script is located
10+
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
12+
13+
# Function to run a test
14+
# Usage: run_test "test_name" "command_to_run"
15+
run_test() {
16+
local test_name=$1
17+
local command=$2
18+
19+
echo -n "- Testing $test_name... "
20+
21+
# Run the command and capture output and exit status
22+
output=$(eval $command 2>&1)
23+
status=$?
24+
25+
if [ $status -eq 0 ]; then
26+
echo "PASSED"
27+
TESTS_PASSED=$((TESTS_PASSED + 1))
28+
else
29+
echo "FAILED"
30+
echo " Command: $command"
31+
echo " Output: $output"
32+
TESTS_FAILED=$((TESTS_FAILED + 1))
33+
fi
34+
35+
return $status
36+
}
37+
38+
# Function to create a temporary directory for tests
39+
# Usage: create_test_dir
40+
create_test_dir() {
41+
mktemp -d -t context-test-XXXXXX
42+
}
43+
44+
# Function to clean up a temporary directory
45+
# Usage: cleanup_test_dir "$test_dir"
46+
cleanup_test_dir() {
47+
local test_dir=$1
48+
rm -rf "$test_dir"
49+
}
50+
51+
# Function to assert that a string contains another string
52+
# Usage: assert_contains "haystack" "needle"
53+
assert_contains() {
54+
local haystack=$1
55+
local needle=$2
56+
57+
if [[ "$haystack" == *"$needle"* ]]; then
58+
return 0
59+
else
60+
return 1
61+
fi
62+
}
63+
64+
# Function to assert that a string does not contain another string
65+
# Usage: assert_not_contains "haystack" "needle"
66+
assert_not_contains() {
67+
local haystack=$1
68+
local needle=$2
69+
70+
if [[ "$haystack" != *"$needle"* ]]; then
71+
return 0
72+
else
73+
return 1
74+
fi
75+
}
76+
77+
# Function to create a test file with a specific size
78+
# Usage: create_test_file "$dir/file.txt" 1024 # Creates a 1KB file
79+
create_test_file() {
80+
local file_path=$1
81+
local size_bytes=$2
82+
83+
dd if=/dev/zero of="$file_path" bs=1 count=$size_bytes status=none
84+
}

test/unit/test_argument_parsing.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# test_argument_parsing.sh - Tests for command-line argument parsing
4+
5+
# Get the directory where this test script is located
6+
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
7+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
8+
9+
# Source test utilities
10+
source "$TEST_DIR/test_utils.sh"
11+
12+
# Create a temporary directory for this test
13+
test_dir=$(create_test_dir)
14+
15+
# Setup test file
16+
echo "// Test file" > "$test_dir/test.js"
17+
18+
# Test 1: Help option
19+
echo "Testing help option"
20+
run_test "help_option" "$PROJECT_ROOT/context --help | grep -q 'Usage:'"
21+
22+
# Test 2: Summary option
23+
echo "Testing summary option"
24+
run_test "summary_option" "cd $test_dir && $PROJECT_ROOT/context --files=test.js --summary | grep -q 'Summary:'"
25+
26+
# Test 3: No ls-files option
27+
echo "Testing no ls-files option"
28+
run_test "no_ls_files_option" "cd $test_dir && $PROJECT_ROOT/context --files=test.js --no-ls-files | grep -qv 'Repository Files'"
29+
30+
# Test 4: Verbose option
31+
echo "Testing verbose option with non-existent file"
32+
output=$(cd $test_dir && $PROJECT_ROOT/context --files=nonexistent.js --verbose 2>&1 || true)
33+
run_test "verbose_option" "echo '$output' | grep -q 'Warning: File not found:'"
34+
35+
# Test 5: Invalid option
36+
echo "Testing invalid option"
37+
run_test "invalid_option" "! $PROJECT_ROOT/context --invalid-option 2>/dev/null"
38+
39+
# Clean up
40+
cleanup_test_dir "$test_dir"

test/unit/test_file_processing.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# test_file_processing.sh - Tests for file processing functionality
4+
5+
# Get the directory where this test script is located
6+
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
7+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
8+
9+
# Source test utilities
10+
source "$TEST_DIR/test_utils.sh"
11+
12+
# Create a temporary directory for this test
13+
test_dir=$(create_test_dir)
14+
15+
# Setup test files
16+
mkdir -p "$test_dir/src"
17+
echo "// Test file 1" > "$test_dir/src/file1.js"
18+
echo "// Test file 2" > "$test_dir/src/file2.js"
19+
mkdir -p "$test_dir/node_modules"
20+
echo "// Should be excluded" > "$test_dir/node_modules/exclude_me.js"
21+
22+
# Test 1: Basic file inclusion
23+
echo "Testing basic file inclusion"
24+
run_test "basic_file_inclusion" "cd $test_dir && $PROJECT_ROOT/context --files=src/file1.js | grep -q 'Test file 1'"
25+
26+
# Test 2: File pattern inclusion
27+
echo "Testing file pattern inclusion"
28+
run_test "file_pattern_inclusion" "cd $test_dir && $PROJECT_ROOT/context --files='src/*.js' | grep -q 'Test file 2'"
29+
30+
# Test 3: Exclude pattern
31+
echo "Testing exclude pattern"
32+
run_test "exclude_pattern" "cd $test_dir && $PROJECT_ROOT/context --files='**/*.js' --exclude='node_modules/**' | grep -qv 'Should be excluded'"
33+
34+
# Test 4: Multiple file patterns
35+
echo "Testing multiple file patterns"
36+
run_test "multiple_file_patterns" "cd $test_dir && $PROJECT_ROOT/context --files='src/file1.js' --files='src/file2.js' | grep -q 'Test file 1' && cd $test_dir && $PROJECT_ROOT/context --files='src/file1.js' --files='src/file2.js' | grep -q 'Test file 2'"
37+
38+
# Clean up
39+
cleanup_test_dir "$test_dir"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# test_size_calculations.sh - Tests for size calculation and truncation
4+
5+
# Get the directory where this test script is located
6+
TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
7+
PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)"
8+
9+
# Source test utilities
10+
source "$TEST_DIR/test_utils.sh"
11+
12+
# Create a temporary directory for this test
13+
test_dir=$(create_test_dir)
14+
15+
# Setup test files
16+
create_test_file "$test_dir/small.txt" 1024 # 1KB file
17+
create_test_file "$test_dir/large.txt" 5120 # 5KB file
18+
19+
# Test 1: Max size limit not exceeded
20+
echo "Testing max size limit not exceeded"
21+
run_test "max_size_not_exceeded" "cd $test_dir && $PROJECT_ROOT/context --files=small.txt --max-size=2KB"
22+
23+
# Test 2: Max size limit exceeded
24+
echo "Testing max size limit exceeded"
25+
run_test "max_size_exceeded" "cd $test_dir && ! $PROJECT_ROOT/context --files=small.txt --files=large.txt --max-size=2KB"
26+
27+
# Test 3: File truncation
28+
echo "Testing file truncation"
29+
result=$(cd $test_dir && $PROJECT_ROOT/context --files=large.txt --truncate-large=2KB)
30+
run_test "file_truncation" "echo '$result' | grep -q '\[File truncated due to size limit\]'"
31+
32+
# Test 4: Human readable size display
33+
echo "Testing human readable size display"
34+
result=$(cd $test_dir && $PROJECT_ROOT/context --files=small.txt --show-file-sizes)
35+
run_test "human_readable_size" "echo '$result' | grep -q 'Size: 1 KB'"
36+
37+
# Clean up
38+
cleanup_test_dir "$test_dir"

0 commit comments

Comments
 (0)