Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules/
.DS_Store
temp-download.zip
temp-extract/
test-projects/
test-manual/
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ This downloads from: `https://github.com/NetFrameworkTemplates/web-netfx`

- Node.js >= 14.0.0

## Testing

Two test scripts are provided to verify the functionality:

### Automated Tests

Run automated tests that verify all scenarios and clean up afterwards:

```bash
./test.sh
```

This tests:
- Creating a project with repo name and ProjectName
- Creating a project with organization/repo and ProjectName
- Creating a project without ProjectName (current directory)
- Error handling for existing directories
- Error handling for non-empty directories

### Manual Tests

Run manual tests that leave the results for inspection:

```bash
./test-manual.sh
```

This creates test projects in `test-manual/` for manual verification. Clean up with `rm -rf test-manual/` when done.

## Publishing

To publish this package to npm:
Expand Down
77 changes: 77 additions & 0 deletions test-manual.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Manual test script for create-net
# This script runs the three main scenarios and leaves the results for manual inspection
# Run this from the create-net project root directory

set -e # Exit on error

YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

TEST_DIR="test-manual"
CREATE_NET_SCRIPT="$(pwd)/bin/create-net.js"

echo -e "${YELLOW}=== Manual Testing for create-net ===${NC}"
echo -e "${YELLOW}Results will be saved in: $TEST_DIR${NC}\n"

# Clean up any previous test runs
if [ -d "$TEST_DIR" ]; then
echo "Cleaning up previous test directory..."
rm -rf "$TEST_DIR"
fi

mkdir -p "$TEST_DIR"
cd "$TEST_DIR"

# Test 1: With repo name and ProjectName
echo -e "\n${YELLOW}================================================${NC}"
echo -e "${GREEN}Test 1: With repo name and ProjectName${NC}"
echo -e "${YELLOW}Command: node $CREATE_NET_SCRIPT nextjs AcmeCorp${NC}"
echo -e "${YELLOW}================================================${NC}\n"

node "$CREATE_NET_SCRIPT" nextjs AcmeCorp

echo -e "\n${GREEN}✓ Test 1 complete. Check $TEST_DIR/AcmeCorp${NC}"

# Test 2: With organization and repo name and ProjectName
echo -e "\n${YELLOW}================================================${NC}"
echo -e "${GREEN}Test 2: With org/repo and ProjectName${NC}"
echo -e "${YELLOW}Command: node $CREATE_NET_SCRIPT NetCoreTemplates/vue-vite VueProject${NC}"
echo -e "${YELLOW}================================================${NC}\n"

node "$CREATE_NET_SCRIPT" NetCoreTemplates/vue-vite VueProject

echo -e "\n${GREEN}✓ Test 2 complete. Check $TEST_DIR/VueProject${NC}"

# Test 3: Without ProjectName (extract to current directory)
echo -e "\n${YELLOW}================================================${NC}"
echo -e "${GREEN}Test 3: Without ProjectName (current directory)${NC}"
echo -e "${YELLOW}Commands:${NC}"
echo -e "${YELLOW} mkdir CurrentDirTest${NC}"
echo -e "${YELLOW} cd CurrentDirTest${NC}"
echo -e "${YELLOW} node $CREATE_NET_SCRIPT nextjs${NC}"
echo -e "${YELLOW}================================================${NC}\n"

mkdir CurrentDirTest
cd CurrentDirTest
node "$CREATE_NET_SCRIPT" nextjs
cd ..

echo -e "\n${GREEN}✓ Test 3 complete. Check $TEST_DIR/CurrentDirTest${NC}"

# Summary
echo -e "\n${YELLOW}================================================${NC}"
echo -e "${GREEN}All manual tests complete!${NC}"
echo -e "${YELLOW}================================================${NC}"
echo -e "\nTest results are in: ${YELLOW}$TEST_DIR${NC}"
echo -e "\nYou can inspect the following directories:"
echo -e " 1. ${YELLOW}$TEST_DIR/AcmeCorp${NC} - Created with repo name and ProjectName"
echo -e " 2. ${YELLOW}$TEST_DIR/VueProject${NC} - Created with org/repo and ProjectName"
echo -e " 3. ${YELLOW}$TEST_DIR/CurrentDirTest${NC} - Created without ProjectName\n"
echo -e "Verify that:"
echo -e " • Files contain project-specific names (AcmeCorp, VueProject, CurrentDirTest)"
echo -e " • 'MyApp' variations have been replaced with project name variations"
echo -e " • node_modules directories exist (npm install ran)\n"
echo -e "To clean up: ${YELLOW}rm -rf $TEST_DIR${NC}\n"
151 changes: 151 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/bin/bash

# Test script for create-net
# Tests different scenarios for creating projects from templates

set -e # Exit on error

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

TEST_DIR="test-projects"
CREATE_NET_SCRIPT="$(pwd)/bin/create-net.js"

echo -e "${YELLOW}=== Testing create-net ===${NC}\n"

# Clean up any previous test runs
if [ -d "$TEST_DIR" ]; then
echo "Cleaning up previous test directory..."
rm -rf "$TEST_DIR"
fi

mkdir -p "$TEST_DIR"
cd "$TEST_DIR"

# Track test results
TESTS_PASSED=0
TESTS_FAILED=0

# Helper function to check if a project was created successfully
check_project() {
local project_name=$1
local project_path=$2

echo -e "\n${YELLOW}Verifying project: $project_name${NC}"

# Check if directory exists
if [ ! -d "$project_path" ]; then
echo -e "${RED}✗ FAILED: Directory $project_path does not exist${NC}"
((TESTS_FAILED++))
return 1
fi

# Check if files were extracted
if [ -z "$(ls -A $project_path)" ]; then
echo -e "${RED}✗ FAILED: Directory $project_path is empty${NC}"
((TESTS_FAILED++))
return 1
fi

# Check if project name replacements were made
# Look for any files containing the project name variations
local found_replacement=false

# Check for lowercase version in files
local lowercase_name=$(echo "$project_name" | tr '[:upper:]' '[:lower:]')
if grep -r "$lowercase_name" "$project_path" --include="*.json" --include="*.md" --include="*.txt" --include="*.cs" --include="*.js" --include="*.ts" >/dev/null 2>&1; then
found_replacement=true
fi

if [ "$found_replacement" = false ]; then
echo -e "${YELLOW}⚠ WARNING: Could not verify project name replacement (this may be normal if template doesn't have MyApp references)${NC}"
fi

echo -e "${GREEN}✓ PASSED: Project $project_name created successfully${NC}"
((TESTS_PASSED++))
return 0
}

# Test 1: Create project with repo name and ProjectName
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test 1: With repo name and ProjectName${NC}"
echo -e "${YELLOW}Command: node $CREATE_NET_SCRIPT nextjs AcmeCorp${NC}"
echo -e "${YELLOW}========================================${NC}\n"

node "$CREATE_NET_SCRIPT" nextjs AcmeCorp
check_project "AcmeCorp" "AcmeCorp"

# Test 2: Create project with organization/repo name and ProjectName
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test 2: With org/repo name and ProjectName${NC}"
echo -e "${YELLOW}Command: node $CREATE_NET_SCRIPT NetCoreTemplates/vue-vite VueProject${NC}"
echo -e "${YELLOW}========================================${NC}\n"

node "$CREATE_NET_SCRIPT" NetCoreTemplates/vue-vite VueProject
check_project "VueProject" "VueProject"

# Test 3: Create project without ProjectName (extract to current directory)
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test 3: Without ProjectName (current directory)${NC}"
echo -e "${YELLOW}Commands: mkdir CurrentDirTest && cd CurrentDirTest && node $CREATE_NET_SCRIPT nextjs${NC}"
echo -e "${YELLOW}========================================${NC}\n"

mkdir CurrentDirTest
cd CurrentDirTest
node "$CREATE_NET_SCRIPT" nextjs
check_project "CurrentDirTest" "."
cd ..

# Test 4: Error case - Directory already exists
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test 4: Error handling - Directory exists${NC}"
echo -e "${YELLOW}Command: node $CREATE_NET_SCRIPT nextjs AcmeCorp (should fail)${NC}"
echo -e "${YELLOW}========================================${NC}\n"

if node "$CREATE_NET_SCRIPT" nextjs AcmeCorp 2>&1 | grep -q "already exists"; then
echo -e "${GREEN}✓ PASSED: Correctly detected existing directory${NC}"
((TESTS_PASSED++))
else
echo -e "${RED}✗ FAILED: Should have detected existing directory${NC}"
((TESTS_FAILED++))
fi

# Test 5: Error case - Non-empty current directory
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test 5: Error handling - Non-empty directory${NC}"
echo -e "${YELLOW}Commands: mkdir NonEmptyTest && cd NonEmptyTest && touch file.txt && node $CREATE_NET_SCRIPT nextjs${NC}"
echo -e "${YELLOW}========================================${NC}\n"

mkdir NonEmptyTest
cd NonEmptyTest
touch file.txt
if node "$CREATE_NET_SCRIPT" nextjs 2>&1 | grep -q "not empty"; then
echo -e "${GREEN}✓ PASSED: Correctly detected non-empty directory${NC}"
((TESTS_PASSED++))
else
echo -e "${RED}✗ FAILED: Should have detected non-empty directory${NC}"
((TESTS_FAILED++))
fi
cd ..

# Summary
echo -e "\n${YELLOW}========================================${NC}"
echo -e "${YELLOW}Test Summary${NC}"
echo -e "${YELLOW}========================================${NC}"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
echo -e "${RED}Failed: $TESTS_FAILED${NC}"

# Clean up
cd ..
echo -e "\n${YELLOW}Cleaning up test directory...${NC}"
rm -rf "$TEST_DIR"

if [ $TESTS_FAILED -eq 0 ]; then
echo -e "\n${GREEN}All tests passed! ✓${NC}\n"
exit 0
else
echo -e "\n${RED}Some tests failed! ✗${NC}\n"
exit 1
fi