Skip to content

Commit 1cc743a

Browse files
committed
Modify tests for setup.sh and update.sh
1 parent a4b4a92 commit 1cc743a

3 files changed

Lines changed: 109 additions & 49 deletions

File tree

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* 1.4.2
2+
- Updated tests for setup.sh and update.sh
3+
14
* 1.4.1
25
- Fixed minor bugs in tests for api_examples.
36
- Added test api_examples/tests/test_get_conversion_upload_summary.py

tests/test_setup.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ if ! bash "${SETUP_SCRIPT_PATH}" --python; then
7777
fi
7878

7979
# Check if directory created (mock clone)
80-
if [[ ! -d "${FAKE_HOME}/gaada/google-ads-python/.git" ]]; then
80+
if [[ ! -d "${FAKE_PROJECT}/client_libs/google-ads-python/.git" ]]; then
8181
echo "FAIL: google-ads-python was not 'cloned' (mocked)"
8282
exit 1
8383
fi
@@ -99,7 +99,7 @@ if ! bash "${SETUP_SCRIPT_PATH}" --java; then
9999
fi
100100

101101
# Check if java directory created
102-
if [[ ! -d "${FAKE_HOME}/gaada/google-ads-java/.git" ]]; then
102+
if [[ ! -d "${FAKE_PROJECT}/client_libs/google-ads-java/.git" ]]; then
103103
echo "FAIL: google-ads-java was not 'cloned'"
104104
exit 1
105105
fi

tests/test_update_logic.sh

Lines changed: 104 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,129 @@
11
#!/bin/bash
22
set -u
33

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"
77

8-
mkdir .gemini
9-
SETTINGS_JSON=".gemini/settings.json"
8+
echo "Running tests in ${TEST_TMP_DIR}"
109

11-
# function to mimic err
12-
err() {
13-
echo "ERROR: $*" >&2
10+
# Cleanup function
11+
cleanup() {
12+
rm -rf "${TEST_TMP_DIR}"
1413
}
14+
trap cleanup EXIT
1515

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}"
3221

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}"
4931
}
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}"
5088

5189
echo "Initial settings:"
5290
cat "${SETTINGS_JSON}"
91+
echo "Initial customer_id:"
92+
cat "${CUSTOMER_ID_FILE}"
5393

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
55101

102+
# 4. Verify Results
56103
echo "Final settings:"
57104
cat "${SETTINGS_JSON}"
105+
echo "Final customer_id:"
106+
cat "${CUSTOMER_ID_FILE}"
58107

59-
# Verify
108+
# Verify Settings
60109
USER_VAL=$(jq -r .user_setting "${SETTINGS_JSON}")
61110
REPO_VAL=$(jq -r .repo_setting "${SETTINGS_JSON}")
62111
COMMON_VAL=$(jq -r .common_setting "${SETTINGS_JSON}")
63112

64113
if [[ "$USER_VAL" == "true" ]] && [[ "$REPO_VAL" == "true" ]] && [[ "$COMMON_VAL" == "user_value" ]]; then
65-
echo "TEST PASSED"
114+
echo "PASS: Settings merged correctly"
66115
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"
71117
exit 1
72118
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

Comments
 (0)