|
1 | 1 | #!/bin/bash |
2 | 2 | set -u |
3 | 3 |
|
4 | | -# Setup environment |
5 | | -TEST_DIR=$(mktemp -d) |
6 | | -cd "${TEST_DIR}" |
| 4 | +# --- Test Update Logic --- |
| 5 | +TEST_TMP_DIR=$(mktemp -d) |
| 6 | +UPDATE_SCRIPT_PATH="$(cd "$(dirname "$0")/.." && pwd)/update.sh" |
7 | 7 |
|
8 | | -mkdir .gemini |
9 | | -SETTINGS_JSON=".gemini/settings.json" |
| 8 | +echo "Running tests in ${TEST_TMP_DIR}" |
10 | 9 |
|
11 | | -# function to mimic err |
12 | | -err() { |
13 | | - echo "ERROR: $*" >&2 |
| 10 | +# Cleanup function |
| 11 | +cleanup() { |
| 12 | + rm -rf "${TEST_TMP_DIR}" |
14 | 13 | } |
| 14 | +trap cleanup EXIT |
15 | 15 |
|
16 | | -# Create "user" settings (simulating existing file) |
17 | | -echo '{"user_setting": true, "common_setting": "user_value"}' > "${SETTINGS_JSON}" |
18 | | - |
19 | | -# Define the logic block to test (extracted from update.sh) |
20 | | -run_update_logic() { |
21 | | - SETTINGS_JSON=".gemini/settings.json" |
22 | | - TEMP_SETTINGS=$(mktemp) |
23 | | - |
24 | | - # 1. Backup existing settings if they exist |
25 | | - if [[ -f "${SETTINGS_JSON}" ]]; then |
26 | | - echo "Backing up ${SETTINGS_JSON}..." |
27 | | - cp "${SETTINGS_JSON}" "${TEMP_SETTINGS}" |
28 | | - |
29 | | - # MOCK: git checkout would go here |
30 | | - echo "Mocking git checkout..." |
31 | | - fi |
| 16 | +# 1. Mock Environment |
| 17 | +FAKE_HOME=$(mktemp -d) |
| 18 | +FAKE_PROJECT=$(mktemp -d) |
| 19 | +echo "FAKE_HOME: ${FAKE_HOME}" |
| 20 | +echo "FAKE_PROJECT: ${FAKE_PROJECT}" |
32 | 21 |
|
33 | | - # MOCK: git pull (simulating update that changes settings.json) |
34 | | - echo "Mocking git pull (updating settings.json)..." |
35 | | - # Overwrite settings.json with "repo" version |
36 | | - echo '{"repo_setting": true, "common_setting": "repo_value"}' > "${SETTINGS_JSON}" |
37 | | - |
38 | | - # 3. Restore/Merge settings |
39 | | - if [[ -f "${TEMP_SETTINGS}" ]] && [[ -s "${TEMP_SETTINGS}" ]]; then |
40 | | - echo "Merging preserved settings with new defaults..." |
41 | | - if jq -s '.[0] * .[1]' "${SETTINGS_JSON}" "${TEMP_SETTINGS}" > "${TEMP_SETTINGS}.merged"; then |
42 | | - mv "${TEMP_SETTINGS}.merged" "${SETTINGS_JSON}" |
43 | | - echo "Settings restored and merged successfully." |
44 | | - else |
45 | | - echo "WARN: Failed to merge settings.json." |
46 | | - fi |
47 | | - rm -f "${TEMP_SETTINGS}" |
48 | | - fi |
| 22 | +export HOME="${FAKE_HOME}" |
| 23 | +mkdir -p "${FAKE_HOME}/bin" |
| 24 | +export PATH="${FAKE_HOME}/bin:${PATH}" |
| 25 | + |
| 26 | +# Cleanup function (updated) |
| 27 | +cleanup() { |
| 28 | + rm -rf "${TEST_TMP_DIR}" |
| 29 | + rm -rf "${FAKE_HOME}" |
| 30 | + rm -rf "${FAKE_PROJECT}" |
49 | 31 | } |
| 32 | +trap cleanup EXIT |
| 33 | + |
| 34 | +# Create mock git |
| 35 | +cat > "${FAKE_HOME}/bin/git" <<EOF |
| 36 | +#!/bin/bash |
| 37 | +if [[ "\$1" == "rev-parse" ]]; then |
| 38 | + # Return the temp dir as the project root |
| 39 | + echo "${FAKE_PROJECT}" |
| 40 | +elif [[ "\$1" == "pull" ]]; then |
| 41 | + echo "Mock pull successful" |
| 42 | + # Simulate update by modifying files ONLY IF we are meant to simulate a repo update? |
| 43 | + # update.sh pulls. If we want to test merging, we need 'git pull' to seemingly update the file. |
| 44 | + # But since we can't easily make 'git pull' actually update a file in this simple mock without a real repo, |
| 45 | + # we might need to rely on the fact that update.sh backs up BEFORE pulling. |
| 46 | + # Wait, update.sh backs up, then pulls, then merges. |
| 47 | + # If 'git pull' doesn't change anything, the merge might be trivial. |
| 48 | + # We want to simulate 'git pull' CHANGING the file to the REPO version. |
| 49 | + |
| 50 | + # Check CWD to verify provided settings.json vs others? |
| 51 | + # For now, let's just create the "repo" version of files here if they exist |
| 52 | + if [[ -f ".gemini/settings.json" ]]; then |
| 53 | + # Simulate repo having new values |
| 54 | + echo '{"repo_setting": true, "common_setting": "repo_value", "context": {"includeDirectories": []}}' > ".gemini/settings.json" |
| 55 | + fi |
| 56 | + # We don't touch customer_id.txt in repo usually, or maybe we do? |
| 57 | + # If repo has customer_id.txt, it might overwrite. |
| 58 | + if [[ -f "customer_id.txt" ]]; then |
| 59 | + echo "REPO_CUSTOMER_ID" > "customer_id.txt" |
| 60 | + fi |
| 61 | +elif [[ "\$1" == "ls-files" ]]; then |
| 62 | + exit 0 # everything matches for now |
| 63 | +elif [[ "\$1" == "checkout" ]]; then |
| 64 | + echo "Mock checkout \$2" |
| 65 | + # Actually restore the file to "HEAD" state? |
| 66 | + # logic: if git ls-files ...; then git checkout ...; fi |
| 67 | + # We can just ignore checkout for this test as we want to test the MERGE/RESTORE logic primarily. |
| 68 | +else |
| 69 | + echo "Mock git: command \$* ignored" |
| 70 | +fi |
| 71 | +EOF |
| 72 | +chmod +x "${FAKE_HOME}/bin/git" |
| 73 | + |
| 74 | +# Create mock jq if not present |
| 75 | +if ! command -v jq &> /dev/null; then |
| 76 | + echo "FAIL: real jq is required for this test" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +# 2. Setup "Project" in Temp Dir |
| 81 | +mkdir -p "${FAKE_PROJECT}/.gemini" |
| 82 | +SETTINGS_JSON="${FAKE_PROJECT}/.gemini/settings.json" |
| 83 | +CUSTOMER_ID_FILE="${FAKE_PROJECT}/customer_id.txt" |
| 84 | + |
| 85 | +# Initial "User" State |
| 86 | +echo '{"user_setting": true, "common_setting": "user_value", "context": {"includeDirectories": []}}' > "${SETTINGS_JSON}" |
| 87 | +echo "USER_CUSTOMER_ID" > "${CUSTOMER_ID_FILE}" |
50 | 88 |
|
51 | 89 | echo "Initial settings:" |
52 | 90 | cat "${SETTINGS_JSON}" |
| 91 | +echo "Initial customer_id:" |
| 92 | +cat "${CUSTOMER_ID_FILE}" |
53 | 93 |
|
54 | | -run_update_logic |
| 94 | +# 3. Run update.sh from within FAKE_PROJECT (update.sh expects to be in repo) |
| 95 | +cd "${FAKE_PROJECT}" |
| 96 | +echo "--- Running update.sh ---" |
| 97 | +if ! bash "${UPDATE_SCRIPT_PATH}"; then |
| 98 | + echo "FAIL: update.sh failed" |
| 99 | + exit 1 |
| 100 | +fi |
55 | 101 |
|
| 102 | +# 4. Verify Results |
56 | 103 | echo "Final settings:" |
57 | 104 | cat "${SETTINGS_JSON}" |
| 105 | +echo "Final customer_id:" |
| 106 | +cat "${CUSTOMER_ID_FILE}" |
58 | 107 |
|
59 | | -# Verify |
| 108 | +# Verify Settings |
60 | 109 | USER_VAL=$(jq -r .user_setting "${SETTINGS_JSON}") |
61 | 110 | REPO_VAL=$(jq -r .repo_setting "${SETTINGS_JSON}") |
62 | 111 | COMMON_VAL=$(jq -r .common_setting "${SETTINGS_JSON}") |
63 | 112 |
|
64 | 113 | if [[ "$USER_VAL" == "true" ]] && [[ "$REPO_VAL" == "true" ]] && [[ "$COMMON_VAL" == "user_value" ]]; then |
65 | | - echo "TEST PASSED" |
| 114 | + echo "PASS: Settings merged correctly" |
66 | 115 | else |
67 | | - echo "TEST FAILED" |
68 | | - echo "user_setting: $USER_VAL (expected true)" |
69 | | - echo "repo_setting: $REPO_VAL (expected true)" |
70 | | - echo "common_setting: $COMMON_VAL (expected user_value)" |
| 116 | + echo "FAIL: Settings merge incorrect" |
71 | 117 | exit 1 |
72 | 118 | fi |
| 119 | + |
| 120 | +# Verify Customer ID |
| 121 | +CID_VAL=$(cat "${CUSTOMER_ID_FILE}") |
| 122 | +if [[ "$CID_VAL" == "USER_CUSTOMER_ID" ]]; then |
| 123 | + echo "PASS: Customer ID preserved" |
| 124 | +else |
| 125 | + echo "FAIL: Customer ID NOT preserved (Got: $CID_VAL)" |
| 126 | + exit 1 |
| 127 | +fi |
| 128 | + |
| 129 | +echo "ALL TESTS PASSED" |
0 commit comments