aboutsummaryrefslogtreecommitdiff
path: root/tests/lib/test_helpers.sh
blob: 726d788deb13ac0328aa92c219a02af6a9ff09cb (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
# ==============================================================================
# Test Helper Functions for rsyncshot
# ==============================================================================

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# Counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0

# Test environment paths
TEST_DIR=""
TEST_CONFIG_DIR=""
TEST_BACKUP_DIR=""
SCRIPT_PATH=""

# ------------------------------------------------------------------------------
# Setup/Teardown
# ------------------------------------------------------------------------------

setup_test_env() {
    # Create temporary directories for testing
    TEST_DIR=$(mktemp -d)
    TEST_CONFIG_DIR="$TEST_DIR/etc/rsyncshot"
    TEST_BACKUP_DIR="$TEST_DIR/backup"
    TEST_SOURCE_DIR="$TEST_DIR/source"

    mkdir -p "$TEST_CONFIG_DIR"
    mkdir -p "$TEST_BACKUP_DIR"
    mkdir -p "$TEST_SOURCE_DIR/home/testuser"
    mkdir -p "$TEST_SOURCE_DIR/etc"

    # Create some test files
    echo "test file 1" > "$TEST_SOURCE_DIR/home/testuser/file1.txt"
    echo "test file 2" > "$TEST_SOURCE_DIR/home/testuser/file2.txt"
    echo "config data" > "$TEST_SOURCE_DIR/etc/test.conf"

    # Find the script (relative to tests directory)
    SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)/rsyncshot"

    if [ ! -f "$SCRIPT_PATH" ]; then
        echo "ERROR: Cannot find rsyncshot script at $SCRIPT_PATH"
        exit 1
    fi
}

teardown_test_env() {
    # Clean up temporary directories
    if [ -n "$TEST_DIR" ] && [ -d "$TEST_DIR" ]; then
        rm -rf "$TEST_DIR"
    fi
}

# Run rsyncshot with test environment variables
# Usage: run_rsyncshot [args...]
run_rsyncshot() {
    INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" "$@"
}

# Create a test config file
create_test_config() {
    local config_file="$TEST_CONFIG_DIR/config"
    cat > "$config_file" << EOF
REMOTE_HOST=""
MOUNTDIR="$TEST_BACKUP_DIR"
EOF
    echo "$config_file"
}

# Create a test include file
create_test_includes() {
    local include_file="$TEST_CONFIG_DIR/include.txt"
    cat > "$include_file" << EOF
$TEST_SOURCE_DIR/home
$TEST_SOURCE_DIR/etc
EOF
    echo "$include_file"
}

# Create a test exclude file
create_test_excludes() {
    local exclude_file="$TEST_CONFIG_DIR/exclude.txt"
    cat > "$exclude_file" << EOF
*.tmp
*.log
.cache
EOF
    echo "$exclude_file"
}

# ------------------------------------------------------------------------------
# Assertions
# ------------------------------------------------------------------------------

assert_equals() {
    local expected="$1"
    local actual="$2"
    local message="${3:-Values should be equal}"

    if [ "$expected" = "$actual" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        echo "  Expected: $expected"
        echo "  Actual:   $actual"
        return 1
    fi
}

assert_exit_code() {
    local expected="$1"
    local actual="$2"
    local message="${3:-Exit code should be $expected}"

    if [ "$expected" -eq "$actual" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        echo "  Expected exit code: $expected"
        echo "  Actual exit code:   $actual"
        return 1
    fi
}

assert_contains() {
    local haystack="$1"
    local needle="$2"
    local message="${3:-Output should contain '$needle'}"

    if echo "$haystack" | grep -q "$needle"; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        echo "  Looking for: $needle"
        echo "  In output:   $haystack"
        return 1
    fi
}

assert_not_contains() {
    local haystack="$1"
    local needle="$2"
    local message="${3:-Output should not contain '$needle'}"

    if ! echo "$haystack" | grep -q "$needle"; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        echo "  Should not contain: $needle"
        echo "  But found in:       $haystack"
        return 1
    fi
}

assert_file_exists() {
    local file="$1"
    local message="${2:-File should exist: $file}"

    if [ -f "$file" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        return 1
    fi
}

assert_dir_exists() {
    local dir="$1"
    local message="${2:-Directory should exist: $dir}"

    if [ -d "$dir" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        return 1
    fi
}

assert_file_not_exists() {
    local file="$1"
    local message="${2:-File should not exist: $file}"

    if [ ! -f "$file" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        return 1
    fi
}

assert_dir_not_exists() {
    local dir="$1"
    local message="${2:-Directory should not exist: $dir}"

    if [ ! -d "$dir" ]; then
        return 0
    else
        echo -e "${RED}FAIL${NC}: $message"
        return 1
    fi
}

# ------------------------------------------------------------------------------
# Test Runner
# ------------------------------------------------------------------------------

run_test() {
    local test_name="$1"
    local test_func="$2"

    TESTS_RUN=$((TESTS_RUN + 1))

    # Run the test function and capture result
    if $test_func; then
        echo -e "${GREEN}PASS${NC}: $test_name"
        TESTS_PASSED=$((TESTS_PASSED + 1))
        return 0
    else
        TESTS_FAILED=$((TESTS_FAILED + 1))
        return 1
    fi
}

print_summary() {
    echo ""
    echo "============================================================"
    echo "Test Summary"
    echo "============================================================"
    echo -e "Total:  $TESTS_RUN"
    echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
    echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
    echo ""

    if [ "$TESTS_FAILED" -eq 0 ]; then
        echo -e "${GREEN}All tests passed!${NC}"
        return 0
    else
        echo -e "${RED}Some tests failed.${NC}"
        return 1
    fi
}