main
1#!/usr/bin/env bash
2# test-package.sh - Enhanced package testing
3#
4# Usage: ./test-package.sh <package-name>
5
6set -euo pipefail
7
8PACKAGE=$1
9
10if [ -z "$PACKAGE" ]; then
11 echo "Usage: $0 <package-name>"
12 exit 1
13fi
14
15echo "=========================================="
16echo "Testing package: $PACKAGE"
17echo "=========================================="
18
19# 1. Build
20echo ""
21echo "==> Building..."
22if ! nix build .#"$PACKAGE" --no-link 2>&1 | tail -5; then
23 echo "ERROR: Build failed"
24 exit 1
25fi
26echo "✓ Build successful"
27
28# 2. Check meta
29echo ""
30echo "==> Checking metadata..."
31META=$(nix eval .#"$PACKAGE".meta --json 2>/dev/null || echo '{}')
32
33# Check description
34if ! echo "$META" | jq -e '.description' > /dev/null 2>&1; then
35 echo "ERROR: Missing description"
36 exit 1
37fi
38DESCRIPTION=$(echo "$META" | jq -r '.description')
39echo " Description: $DESCRIPTION"
40
41# Check license
42if ! echo "$META" | jq -e '.license' > /dev/null 2>&1; then
43 echo "WARNING: Missing license"
44else
45 LICENSE=$(echo "$META" | jq -r '.license.spdxId // .license.shortName // .license')
46 echo " License: $LICENSE"
47fi
48
49# Check platforms
50if ! echo "$META" | jq -e '.platforms' > /dev/null 2>&1; then
51 echo "WARNING: Missing platforms"
52else
53 PLATFORM_COUNT=$(echo "$META" | jq '.platforms | length')
54 echo " Platforms: $PLATFORM_COUNT supported"
55fi
56
57# Check mainProgram
58if echo "$META" | jq -e '.mainProgram' > /dev/null 2>&1; then
59 MAIN_PROGRAM=$(echo "$META" | jq -r '.mainProgram')
60 echo " Main program: $MAIN_PROGRAM"
61fi
62
63echo "✓ Metadata complete"
64
65# 3. Test binary (if executable)
66echo ""
67echo "==> Testing binary..."
68BUILD_RESULT=$(nix build .#"$PACKAGE" --no-link --print-out-paths 2>/dev/null)
69
70if [ -d "$BUILD_RESULT/bin" ]; then
71 BINARIES=$(ls "$BUILD_RESULT/bin")
72 echo " Found binaries: $BINARIES"
73
74 for BIN in $BINARIES; do
75 BIN_PATH="$BUILD_RESULT/bin/$BIN"
76 if [ -x "$BIN_PATH" ]; then
77 echo " Testing: $BIN"
78
79 # Try --version (with timeout)
80 if timeout 2s "$BIN_PATH" --version >/dev/null 2>&1; then
81 VERSION=$(timeout 2s "$BIN_PATH" --version 2>&1 | head -1)
82 echo " --version: $VERSION"
83 # Try --help (with timeout)
84 elif timeout 2s "$BIN_PATH" --help >/dev/null 2>&1; then
85 echo " --help: Available"
86 # Try -h (with timeout)
87 elif timeout 2s "$BIN_PATH" -h >/dev/null 2>&1; then
88 echo " -h: Available"
89 else
90 echo " WARNING: No --version, --help, or -h support"
91 fi
92 fi
93 done
94 echo "✓ Binary tests completed"
95else
96 echo " No binaries found (library or data package)"
97fi
98
99# 4. Check dependencies
100echo ""
101echo "==> Checking dependencies..."
102DEP_COUNT=$(nix path-info -r .#"$PACKAGE" 2>/dev/null | wc -l)
103echo " Runtime dependencies: $DEP_COUNT packages"
104
105# Show largest dependencies
106echo " Largest dependencies:"
107nix path-info -rsSh .#"$PACKAGE" 2>/dev/null | sort -hr | head -5 | sed 's/^/ /'
108
109echo "✓ Dependencies checked"
110
111# 5. Test in clean shell
112echo ""
113echo "==> Testing in clean environment..."
114if [ -d "$BUILD_RESULT/bin" ]; then
115 for BIN in "$BUILD_RESULT"/bin/*; do
116 BIN_NAME=$(basename "$BIN")
117 if nix shell .#"$PACKAGE" --command which "$BIN_NAME" >/dev/null 2>&1; then
118 echo " ✓ $BIN_NAME available in PATH"
119 else
120 echo " ERROR: $BIN_NAME not found in PATH"
121 exit 1
122 fi
123 done
124else
125 echo " Skipping (no binaries)"
126fi
127
128echo ""
129echo "=========================================="
130echo "All tests passed for $PACKAGE!"
131echo "=========================================="