-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·133 lines (104 loc) · 3.36 KB
/
test.sh
File metadata and controls
executable file
·133 lines (104 loc) · 3.36 KB
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
#!/usr/bin/env bash
# A hermetic test to validate changes and demonstrate output.
set -e # Exit on error.
test -f ./ocd-install.sh || \
(echo 'Run this in the same dir as ocd-install.sh' && exit 1)
# Use tmpdirs for testing.
FAKE_HOME="$(mktemp -d)"
TEST_REPO_DIR="$(mktemp -d)"
OCD_HOME="$FAKE_HOME" # For safety because I rm -rf $FAKE_HOME below.
OCD_BARE="${OCD_HOME}/.ocd"
# Create a friendly symlink for clearer example output.
SAMPLE_DOTFILES="/tmp/sample-dotfiles"
rm -f "$SAMPLE_DOTFILES" # Remove any existing symlink.
ln -s "$TEST_REPO_DIR" "$SAMPLE_DOTFILES"
TEST_REMOTE="${SAMPLE_DOTFILES}/dotfiles.git"
cleanup() {
rm -rf "$FAKE_HOME" "$TEST_REPO_DIR"
rm -f "$SAMPLE_DOTFILES"
}
trap cleanup EXIT SIGINT SIGTERM
# Create a minimal test repository.
echo "[*] Creating test repository..."
(
cd "$TEST_REPO_DIR"
mkdir source && cd source
git init -q
git config user.email "test@example.com"
git config user.name "Test User"
# Create a single example dotfile.
cat > .bashrc_example << 'EOF'
# Example bashrc for OCD testing
# This file demonstrates dotfile management with OCD
export OCD_TEST="Hello from OCD!"
alias ocd-test='echo "OCD is working!"'
EOF
git add .bashrc_example
git commit -q -m "Add example bashrc for OCD testing"
cd ..
git clone -q --bare source dotfiles.git
)
OCD="git --git-dir=$OCD_BARE --work-tree=$FAKE_HOME"
EXAMPLE_FILE="example.md"
# Write the initial part of example.md to show how the script is invoked.
cat > "$EXAMPLE_FILE" << 'END'
# Example: `ocd-install.sh`
The following shows console output from `ocd-install.sh` (via
the [test script](./test.sh)) to demonstrate what the setup looks like.
Note: This example uses a local test repository. In practice, you would use
your own dotfiles repository (e.g., `git@github.com:USER/dotfiles.git`).
```
$ ./ocd-install.sh -r <YOUR_DOTFILES_REPO> -c -h -g
END
# Run ocd-install.sh with flags, capturing output in example.md.
{
./ocd-install.sh -r "file://${TEST_REMOTE}" \
-c -h -g -B "$OCD_BARE" -H "$OCD_HOME"
echo '```';
} &>> "$EXAMPLE_FILE"
cat >> "$EXAMPLE_FILE" << 'END'
Running `ocd status` after modifying tracked file `.bashrc_example`:
```
$ ocd status
END
echo "# Modified by test" >> $OCD_HOME/.bashrc_example
{
$OCD status;
cat << END
\`\`\`
And then \`ocd add\` and \`ocd commit\`:
\`\`\`
$ ocd add $OCD_HOME/.bashrc_example
$ ocd commit -m 'Testing OCD'
END
$OCD add "$OCD_HOME/.bashrc_example";
$OCD commit -m 'Testing OCD';
echo '```';
} >> "$EXAMPLE_FILE"
echo "OK: ocd-install.sh"
# Spot-check .gitignore_ocd for a known SSH rule.
if ! grep -q '^.ssh/id_' "$FAKE_HOME/.gitignore_ocd"; then
echo "[!] .gitignore_ocd is missing some rules." && exit 1
else
echo "OK: .gitignore_ocd"
fi
# Confirm the pre-commit hook is in place.
if [ ! -f "$OCD_BARE/hooks/pre-commit" ]; then
echo "[!] Missing pre-commit hook at $OCD_BARE/hooks/pre-commit" && exit 1
else
echo "OK: $OCD_BARE/hooks/pre-commit"
fi
# Test the pre-commit hook.
for i in $(seq 1 22); do
touch "$FAKE_HOME/testfile$i"
done
$OCD add $FAKE_HOME/testfile*
if $OCD commit -m 'big commit' >/dev/null 2>&1; then
echo "[!] Pre-commit hook did not block a large commit." && exit 1
else
echo "OK: pre-commit hook blocked large commit."
fi
# Clean up staged files and temp files.
$OCD reset --quiet HEAD
rm $FAKE_HOME/testfile*
echo -e "\n[*] Don't forget to add example.md to your commit!"