|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Test script for create-net |
| 4 | +# Tests different scenarios for creating projects from templates |
| 5 | + |
| 6 | +set -e# Exit on error |
| 7 | + |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +NC='\033[0m'# No Color |
| 12 | + |
| 13 | +TEST_DIR="test-projects" |
| 14 | +CREATE_NET_SCRIPT="$(pwd)/bin/create-net.js" |
| 15 | + |
| 16 | +echo -e"${YELLOW}=== Testing create-net ===${NC}\n" |
| 17 | + |
| 18 | +# Clean up any previous test runs |
| 19 | +if [-d"$TEST_DIR" ];then |
| 20 | +echo"Cleaning up previous test directory..." |
| 21 | + rm -rf"$TEST_DIR" |
| 22 | +fi |
| 23 | + |
| 24 | +mkdir -p"$TEST_DIR" |
| 25 | +cd"$TEST_DIR" |
| 26 | + |
| 27 | +# Track test results |
| 28 | +TESTS_PASSED=0 |
| 29 | +TESTS_FAILED=0 |
| 30 | + |
| 31 | +# Helper function to check if a project was created successfully |
| 32 | +check_project() { |
| 33 | +local project_name=$1 |
| 34 | +local project_path=$2 |
| 35 | + |
| 36 | +echo -e"\n${YELLOW}Verifying project:$project_name${NC}" |
| 37 | + |
| 38 | +# Check if directory exists |
| 39 | +if [!-d"$project_path" ];then |
| 40 | +echo -e"${RED}✗ FAILED: Directory$project_path does not exist${NC}" |
| 41 | +((TESTS_FAILED++)) |
| 42 | +return 1 |
| 43 | +fi |
| 44 | + |
| 45 | +# Check if files were extracted |
| 46 | +if [-z"$(ls -A$project_path)" ];then |
| 47 | +echo -e"${RED}✗ FAILED: Directory$project_path is empty${NC}" |
| 48 | +((TESTS_FAILED++)) |
| 49 | +return 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Check if project name replacements were made |
| 53 | +# Look for any files containing the project name variations |
| 54 | +local found_replacement=false |
| 55 | + |
| 56 | +# Check for lowercase version in files |
| 57 | +local lowercase_name=$(echo"$project_name"| tr'[:upper:]''[:lower:]') |
| 58 | +if grep -r"$lowercase_name""$project_path" --include="*.json" --include="*.md" --include="*.txt" --include="*.cs" --include="*.js" --include="*.ts">/dev/null2>&1;then |
| 59 | + found_replacement=true |
| 60 | +fi |
| 61 | + |
| 62 | +if ["$found_replacement"=false ];then |
| 63 | +echo -e"${YELLOW}⚠ WARNING: Could not verify project name replacement (this may be normal if template doesn't have MyApp references)${NC}" |
| 64 | +fi |
| 65 | + |
| 66 | +echo -e"${GREEN}✓ PASSED: Project$project_name created successfully${NC}" |
| 67 | +((TESTS_PASSED++)) |
| 68 | +return 0 |
| 69 | +} |
| 70 | + |
| 71 | +# Test 1: Create project with repo name and ProjectName |
| 72 | +echo -e"\n${YELLOW}========================================${NC}" |
| 73 | +echo -e"${YELLOW}Test 1: With repo name and ProjectName${NC}" |
| 74 | +echo -e"${YELLOW}Command: node$CREATE_NET_SCRIPT nextjs AcmeCorp${NC}" |
| 75 | +echo -e"${YELLOW}========================================${NC}\n" |
| 76 | + |
| 77 | +node"$CREATE_NET_SCRIPT" nextjs AcmeCorp |
| 78 | +check_project"AcmeCorp""AcmeCorp" |
| 79 | + |
| 80 | +# Test 2: Create project with organization/repo name and ProjectName |
| 81 | +echo -e"\n${YELLOW}========================================${NC}" |
| 82 | +echo -e"${YELLOW}Test 2: With org/repo name and ProjectName${NC}" |
| 83 | +echo -e"${YELLOW}Command: node$CREATE_NET_SCRIPT NetCoreTemplates/vue-vite VueProject${NC}" |
| 84 | +echo -e"${YELLOW}========================================${NC}\n" |
| 85 | + |
| 86 | +node"$CREATE_NET_SCRIPT" NetCoreTemplates/vue-vite VueProject |
| 87 | +check_project"VueProject""VueProject" |
| 88 | + |
| 89 | +# Test 3: Create project without ProjectName (extract to current directory) |
| 90 | +echo -e"\n${YELLOW}========================================${NC}" |
| 91 | +echo -e"${YELLOW}Test 3: Without ProjectName (current directory)${NC}" |
| 92 | +echo -e"${YELLOW}Commands: mkdir CurrentDirTest && cd CurrentDirTest && node$CREATE_NET_SCRIPT nextjs${NC}" |
| 93 | +echo -e"${YELLOW}========================================${NC}\n" |
| 94 | + |
| 95 | +mkdir CurrentDirTest |
| 96 | +cd CurrentDirTest |
| 97 | +node"$CREATE_NET_SCRIPT" nextjs |
| 98 | +check_project"CurrentDirTest""." |
| 99 | +cd .. |
| 100 | + |
| 101 | +# Test 4: Error case - Directory already exists |
| 102 | +echo -e"\n${YELLOW}========================================${NC}" |
| 103 | +echo -e"${YELLOW}Test 4: Error handling - Directory exists${NC}" |
| 104 | +echo -e"${YELLOW}Command: node$CREATE_NET_SCRIPT nextjs AcmeCorp (should fail)${NC}" |
| 105 | +echo -e"${YELLOW}========================================${NC}\n" |
| 106 | + |
| 107 | +if node"$CREATE_NET_SCRIPT" nextjs AcmeCorp2>&1| grep -q"already exists";then |
| 108 | +echo -e"${GREEN}✓ PASSED: Correctly detected existing directory${NC}" |
| 109 | +((TESTS_PASSED++)) |
| 110 | +else |
| 111 | +echo -e"${RED}✗ FAILED: Should have detected existing directory${NC}" |
| 112 | +((TESTS_FAILED++)) |
| 113 | +fi |
| 114 | + |
| 115 | +# Test 5: Error case - Non-empty current directory |
| 116 | +echo -e"\n${YELLOW}========================================${NC}" |
| 117 | +echo -e"${YELLOW}Test 5: Error handling - Non-empty directory${NC}" |
| 118 | +echo -e"${YELLOW}Commands: mkdir NonEmptyTest && cd NonEmptyTest && touch file.txt && node$CREATE_NET_SCRIPT nextjs${NC}" |
| 119 | +echo -e"${YELLOW}========================================${NC}\n" |
| 120 | + |
| 121 | +mkdir NonEmptyTest |
| 122 | +cd NonEmptyTest |
| 123 | +touch file.txt |
| 124 | +if node"$CREATE_NET_SCRIPT" nextjs2>&1| grep -q"not empty";then |
| 125 | +echo -e"${GREEN}✓ PASSED: Correctly detected non-empty directory${NC}" |
| 126 | +((TESTS_PASSED++)) |
| 127 | +else |
| 128 | +echo -e"${RED}✗ FAILED: Should have detected non-empty directory${NC}" |
| 129 | +((TESTS_FAILED++)) |
| 130 | +fi |
| 131 | +cd .. |
| 132 | + |
| 133 | +# Summary |
| 134 | +echo -e"\n${YELLOW}========================================${NC}" |
| 135 | +echo -e"${YELLOW}Test Summary${NC}" |
| 136 | +echo -e"${YELLOW}========================================${NC}" |
| 137 | +echo -e"${GREEN}Passed:$TESTS_PASSED${NC}" |
| 138 | +echo -e"${RED}Failed:$TESTS_FAILED${NC}" |
| 139 | + |
| 140 | +# Clean up |
| 141 | +cd .. |
| 142 | +echo -e"\n${YELLOW}Cleaning up test directory...${NC}" |
| 143 | +rm -rf"$TEST_DIR" |
| 144 | + |
| 145 | +if [$TESTS_FAILED-eq 0 ];then |
| 146 | +echo -e"\n${GREEN}All tests passed! ✓${NC}\n" |
| 147 | +exit 0 |
| 148 | +else |
| 149 | +echo -e"\n${RED}Some tests failed! ✗${NC}\n" |
| 150 | +exit 1 |
| 151 | +fi |