summaryrefslogtreecommitdiff
path: root/test/test_ansi.sh
blob: e1e95f018578b0326c6c8f103f24b26a89e0fe0f (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
248
249
250
251
252
253
254
255
256
#!/usr/bin/env bash

# wgetpaste test script (stripping ANSI codes)
# Based on test/test.sh
# Exit code: number of mismatched downloads or 1 for general failure
# Copyright (C) 2022 Oskari Pirhonen <xxc3ncoredxx@gmail.com>

# Don't assume the test is being run from the same directory as the script
TEST_DIR="$(dirname "$0")"
ANSI_FILE="$TEST_DIR/red.txt"
NOANSI_FILE="$TEST_DIR/red_no_ansi.txt"
DL_DIR="$(mktemp -q --tmpdir -d wgetpaste_test_ansi.XXXXX)"
# Services to hard skip
# Pre-declare as map to maintain type even if empty
# key -> value := service -> reason
declare -A HARD_SKIPS
HARD_SKIPS=(['codepad']='always times out')
# Services expected to require an authorization token
AUTH_SKIPS=('gists' 'snippets')
# Used to save the first working service
WORKING=
FAILED_PASTE=0
DL_MISMATCH=0

# Test that temp directory was created
if [ ! -d "$DL_DIR" ]; then
    echo "Failed to create temporary download directory: $DL_DIR"
    exit 1
fi
echo "Using download directory: $DL_DIR"

# Post test file into each service until one succeeds
for serv in $("$TEST_DIR"/../wgetpaste -S --completions); do
    # Hard skips
    for hs in "${!HARD_SKIPS[@]}"; do
        if [ "$serv" == "$hs" ]; then
            echo "HARD SKIP on $serv -- reason: ${HARD_SKIPS[$serv]}"
            continue 2
        fi
    done

    # Log errors to analyze the reason
    # Use verbose output to get more meaningful errors
    # Log deleted at the end of each loop unless error other than 401
    echo -n "Posting to $serv: "
    ERROR_LOG="$DL_DIR/$serv-error.log"
    URL="$("$TEST_DIR"/../wgetpaste -r -s "$serv" -v "$ANSI_FILE" 2>"$ERROR_LOG")"
    STATUS="$?"

    # Skip failed posts (eg, not authorized for GitHub/GitLab, service error)
    if [ "$STATUS" -ne 0 ]; then
        if (grep -iq "HTTP.*401.*Unauthorized" "$ERROR_LOG"); then
            # Check if a 401 is expected behavior. If it isn't, mark as fail
            for as in "${AUTH_SKIPS[@]}"; do
                if [ "$serv" == "$as" ]; then
                    echo "SKIPPING, needs authorization..."
                    rm "$ERROR_LOG"
                    continue 2
                fi
            done
            echo "UNEXPECTED 401, skipping..."
        else
            echo "SKIPPING, failed to post..."
        fi

        continue
    fi
    echo "SUCCESS!"

    echo -n "Downloading from $serv: "
    if ! (wget -q "$URL" -O "/dev/null" 2>>"$ERROR_LOG"); then
        echo "FAILED, skipping..."
        continue
    fi
    echo "SUCCESS!"
    rm "$ERROR_LOG"

    # This is the service we want to use
    echo "Using service $serv"
    WORKING="$serv"
    break
done

# Test if we have a working service
if [ -z "$WORKING" ]; then
    echo "No working service found!"
    for log in "$DL_DIR"/*.log; do
        echo "$(basename "$log"):"
        cat "$log"
    done
    rm -rf "$DL_DIR"
    exit 1
fi

# Paste stuff. Use a short timeout between requests (we're friendly after all!)
sleep 1
echo -n "Pasting command output with ANSI stripping (cat): "
ERROR_LOG="$DL_DIR/command-noansi-error.log"
URL="$("$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v -c "cat $ANSI_FILE" 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/command-noansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS"
        rm "$ERROR_LOG"

        echo "Removing 'command run' header"
        sed -i -e '1d' "$DL_DIR/command-noansi.txt"
    fi
fi
sleep 1
echo -n "Pasting command output without ANSI stripping (cat): "
ERROR_LOG="$DL_DIR/command-ansi-error.log"
URL="$("$TEST_DIR"/../wgetpaste -A -r -s "$WORKING" -v -c "cat $ANSI_FILE" 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/command-ansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS"
        rm "$ERROR_LOG"

        echo "Removing 'command run' header"
        sed -i -e '1d' "$DL_DIR/command-ansi.txt"
    fi
fi

sleep 1
echo -n "Pasting stdin with ANSI stripping (cat | wgetpaste): "
ERROR_LOG="$DL_DIR/stdin-noansi-error.log"
URL="$(cat "$ANSI_FILE" | "$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/stdin-noansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS!"
        rm "$ERROR_LOG"
    fi
fi
sleep 1
echo -n "Pasting stdin without ANSI stripping (cat | wgetpaste): "
ERROR_LOG="$DL_DIR/stdin-ansi-error.log"
URL="$(cat "$ANSI_FILE" | "$TEST_DIR"/../wgetpaste -A -r -s "$WORKING" -v 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/stdin-ansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS!"
        rm "$ERROR_LOG"
    fi
fi

sleep 1
echo -n "Pasting a file with ANSI stripping: "
ERROR_LOG="$DL_DIR/file-noansi-error.log"
URL="$("$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v "$ANSI_FILE" 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/file-noansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS!"
        rm "$ERROR_LOG"
    fi
fi
sleep 1
echo -n "Pasting a file without ANSI stripping: "
ERROR_LOG="$DL_DIR/file-ansi-error.log"
URL="$("$TEST_DIR"/../wgetpaste -A -r -s "$WORKING" -v "$ANSI_FILE" 2>"$ERROR_LOG")"
if [ $? -ne 0 ]; then
    echo "FAILED!"
    FAILED_PASTE=$((FAILED_PASTE + 1))
else
    echo "SUCCESS!"

    echo -n "Downloading: "
    if ! (wget -q "$URL" -O "$DL_DIR/file-ansi.txt" 2>>"$ERROR_LOG"); then
        echo "FAILED!"
        FAILED_PASTE=$((FAILED_PASTE + 1))
    else
        echo "SUCCESS!"
        rm "$ERROR_LOG"
    fi
fi

# Compare downloaded files
for dl_file in "$DL_DIR"/*-noansi.txt; do
    echo -n "Testing file $dl_file: "
    # Ignore missing trailing newline and extra empty lines in downloaded file
    if (diff -q -Z -B "$NOANSI_FILE" "$dl_file" &>/dev/null); then
        echo "SUCCESS!"
    else
        echo "FAILED!"
        DL_MISMATCH=$((DL_MISMATCH + 1))
    fi
done
for dl_file in "$DL_DIR"/*-ansi.txt; do
    echo -n "Testing file $dl_file: "
    # Ignore missing trailing newline and extra empty lines in downloaded file
    if (diff -q -Z -B "$ANSI_FILE" "$dl_file" &>/dev/null); then
        echo "SUCCESS!"
    else
        echo "FAILED!"
        DL_MISMATCH=$((DL_MISMATCH + 1))
    fi
done

echo "Total failed pastes: $FAILED_PASTE"
echo "Total mismatches: $DL_MISMATCH"

# Print failure logs
if [ $FAILED_PASTE -ne 0 ]; then
    for log in "$DL_DIR"/*.log; do
        echo "$(basename "$log"):"
        cat "$log"
    done
# Delete download directory if all tests succeeded
elif [ $DL_MISMATCH -eq 0 ]; then
    echo "Deleting download directory"
    rm -rf "$DL_DIR"
fi

exit "$DL_MISMATCH"