-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_replace_env.sh
executable file
·168 lines (129 loc) · 6.6 KB
/
test_replace_env.sh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# Function to set up test environment
setup() {
# Create a temporary directory for testing
TMP_DIR=$(mktemp -d)
cp replace_env.sh "$TMP_DIR/"
cd "$TMP_DIR"
}
# Function to clean up test environment
teardown() {
# Remove the temporary directory after tests
rm -rf "$TMP_DIR"
}
# Function to run a single test
run_test() {
local description="$1"
local command="$2"
local expected="$3"
local result
echo "Running test: $description"
result=$(eval "$command")
if [ "$result" == "$expected" ]; then
echo "Test passed"
else
echo "Test failed"
echo "Expected: $expected"
echo "Got: $result"
exit 1
fi
}
# Function to check file permissions
check_permissions() {
local original_file="$1"
local modified_file="$2"
original_permissions=$(ls -l "$original_file" | awk '{print $1}')
modified_permissions=$(ls -l "$modified_file" | awk '{print $1}')
if [ "$original_permissions" == "$modified_permissions" ]; then
echo "Permissions match: $original_permissions"
else
echo "Permission mismatch: original $original_permissions, modified $modified_permissions"
exit 1
fi
}
# Set up the test environment
setup
# Test 1: Single file replacement
echo "DB_PASSWORD=mysecretpassword" > .env
echo "API_URL=https://api.example.com" >> .env
mkdir -p configs
echo "db_password=\${DB_PASSWORD}" > configs/app.conf
echo "api_url=\${API_URL}" >> configs/app.conf
./replace_env.sh .env configs/app.conf
run_test "Single file replacement - db_password" "grep -o 'db_password=mysecretpassword' configs/app.conf" "db_password=mysecretpassword"
run_test "Single file replacement - api_url" "grep -o 'api_url=https://api.example.com' configs/app.conf" "api_url=https://api.example.com"
# Test 2: Directory recursive replacement
echo "DB_PASSWORD=mysecretpassword" > .env
echo "API_URL=https://api.example.com" >> .env
mkdir -p configs/nested
echo "db_password=\${DB_PASSWORD}" > configs/app.conf
echo "api_url=\${API_URL}" >> configs/app.conf
echo "db_password=\${DB_PASSWORD}" > configs/nested/config1.conf
echo "api_url=\${API_URL}" > configs/nested/config2.conf
./replace_env.sh .env configs
run_test "Directory recursive replacement - app.conf db_password" "grep -o 'db_password=mysecretpassword' configs/app.conf" "db_password=mysecretpassword"
run_test "Directory recursive replacement - app.conf api_url" "grep -o 'api_url=https://api.example.com' configs/app.conf" "api_url=https://api.example.com"
run_test "Directory recursive replacement - config1.conf db_password" "grep -o 'db_password=mysecretpassword' configs/nested/config1.conf" "db_password=mysecretpassword"
run_test "Directory recursive replacement - config2.conf api_url" "grep -o 'api_url=https://api.example.com' configs/nested/config2.conf" "api_url=https://api.example.com"
# Test 3: Specify output file
echo "DB_PASSWORD=mysecretpassword" > .env
echo "API_URL=https://api.example.com" >> .env
mkdir -p configs
echo "db_password=\${DB_PASSWORD}" > configs/app.conf
echo "api_url=\${API_URL}" >> configs/app.conf
./replace_env.sh .env configs/app.conf configs/app_updated.conf
run_test "Specify output file - app_updated.conf db_password" "grep -o 'db_password=mysecretpassword' configs/app_updated.conf" "db_password=mysecretpassword"
run_test "Specify output file - app_updated.conf api_url" "grep -o 'api_url=https://api.example.com' configs/app_updated.conf" "api_url=https://api.example.com"
run_test "Specify output file - original app.conf db_password" "grep -o 'db_password=\${DB_PASSWORD}' configs/app.conf" "db_password=\${DB_PASSWORD}"
run_test "Specify output file - original app.conf api_url" "grep -o 'api_url=\${API_URL}' configs/app.conf" "api_url=\${API_URL}"
# Test 4: Safe replacement example
echo "DB_PASSWORD=mysecretpassword" > .env
echo "API_URL=https://api.example.com" >> .env
mkdir -p configs
echo "db_password=\${DB_PASSWORD}" > configs/app_safe.conf
echo "api_url=\${API_URL}" >> configs/app_safe.conf
echo "other_var=\${OTHER_VAR}" >> configs/app_safe.conf
./replace_env.sh .env configs/app_safe.conf
run_test "Safe replacement - db_password" "grep -o 'db_password=mysecretpassword' configs/app_safe.conf" "db_password=mysecretpassword"
run_test "Safe replacement - api_url" "grep -o 'api_url=https://api.example.com' configs/app_safe.conf" "api_url=https://api.example.com"
run_test "Safe replacement - other_var" "grep -o 'other_var=\${OTHER_VAR}' configs/app_safe.conf" "other_var=\${OTHER_VAR}"
# Test 5: .env without a newline at the end
echo "DB_PASSWORD=mysecretpassword" > .env
echo "API_URL=https://api.example.com" >> .env
# No trailing newline, use printf to avoid the newline
printf "LAST_VAR=value" >> .env
mkdir -p configs
echo "db_password=\${DB_PASSWORD}" > configs/app_no_newline.conf
echo "api_url=\${API_URL}" >> configs/app_no_newline.conf
echo "last_var=\${LAST_VAR}" >> configs/app_no_newline.conf
./replace_env.sh .env configs/app_no_newline.conf
run_test "No newline at end of .env - db_password" "grep -o 'db_password=mysecretpassword' configs/app_no_newline.conf" "db_password=mysecretpassword"
run_test "No newline at end of .env - api_url" "grep -o 'api_url=https://api.example.com' configs/app_no_newline.conf" "api_url=https://api.example.com"
run_test "No newline at end of .env - last_var" "grep -o 'last_var=value' configs/app_no_newline.conf" "last_var=value"
# New tests for permissions
echo "Testing permissions..."
# Test 6: Different permissions for generated files
# Create a source file with specific permissions
touch configs/app_perm_test.conf
chmod 640 configs/app_perm_test.conf
echo "db_password=\${DB_PASSWORD}" > configs/app_perm_test.conf
# Store original permissions
original_perm_perm_test=$(ls -l configs/app_perm_test.conf | awk '{print $1}')
# Run the replacement
./replace_env.sh .env configs/app_perm_test.conf configs/app_perm_test_updated.conf
# Check if the updated file has the same permissions
check_permissions configs/app_perm_test.conf configs/app_perm_test_updated.conf
# Test 7: Ensure updated file retains permissions
# Create another source file with specific permissions
touch configs/app_perm_test2.conf
chmod 600 configs/app_perm_test2.conf
echo "api_url=\${API_URL}" > configs/app_perm_test2.conf
# Store original permissions
original_perm_perm_test2=$(ls -l configs/app_perm_test2.conf | awk '{print $1}')
# Run the replacement
./replace_env.sh .env configs/app_perm_test2.conf configs/app_perm_test2_updated.conf
# Check if the updated file has the same permissions
check_permissions configs/app_perm_test2.conf configs/app_perm_test2_updated.conf
# Clean up the test environment
teardown
echo "All tests passed"