-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCreateTestFiles.sh
executable file
·167 lines (95 loc) · 3.27 KB
/
CreateTestFiles.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
#!/bin/bash
# This script generates test files for script RecompressSelectively.sh
#
# Copyright (c) 2020 R. Diez - Licensed under the GNU AGPLv3
set -o errexit
set -o nounset
set -o pipefail
declare -r -i EXIT_CODE_ERROR=1
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit $EXIT_CODE_ERROR
}
create_pseudorandom_file ()
{
local -r FILENAME="$1"
local -r -i KB_COUNT="$2"
# Each line has random digits from 0 to 9.
# This data will compress with zip at around 50%.
local -r -i LINE_LEN=99
local -i INDEX
local STR
for (( INDEX = 0 ; INDEX < LINE_LEN; ++INDEX )); do
STR+="\$(( RANDOM % 10 ))"
done
local -r -i LINE_COUNT="$(( KB_COUNT * 10 ))"
{
for (( LINE_INDEX = 0 ; LINE_INDEX < LINE_COUNT ; ++LINE_INDEX )); do
eval "echo $STR"
done
} >"$FILENAME"
}
create_test_subdir ()
{
local -r SUBDIR="$1"
local -r EXTRA_FILENAME="$2"
local -r EXTRA_FILE_CONTENTS="$3"
echo "Creating test subdir $SUBDIR ..."
local DELETE_TMP_DIR_CMD
printf -v DELETE_TMP_DIR_CMD "rm -rf %q" "$TMP_DIR_ABS"
eval "$DELETE_TMP_DIR_CMD"
mkdir --parents "$TMP_DIR_ABS"
pushd "$TMP_DIR_ABS" >/dev/null
create_pseudorandom_file "$SUBDIR-data.txt" "100"
echo "Contents of SomeOtherFile.txt ." >"SomeOtherFile.txt"
echo "$EXTRA_FILE_CONTENTS" >"$EXTRA_FILENAME"
zip --quiet --recurse-paths -9 "$SUBDIR-data.zip" .
popd >/dev/null
mkdir -- "$SUBDIR"
mv -- "$TMP_DIR_ABS/$SUBDIR-data.zip" "$SUBDIR/"
eval "$DELETE_TMP_DIR_CMD"
}
if (( $# != 1 )); then
abort "Invalid number of command-line arguments. See this tool's source code for more information."
fi
# Make the pseudorandom data repeatable.
RANDOM=1
declare -r DEST_DIR="$1"
rm -rf -- "$DEST_DIR"
mkdir --parents -- "$DEST_DIR"
pushd "$DEST_DIR" >/dev/null
declare -r DEST_DIR_ABS="$PWD"
declare -r TMP_SUBDIR="tmp"
declare -r TMP_DIR_ABS="$DEST_DIR_ABS/$TMP_SUBDIR"
mkdir "FilesToProcess"
pushd "FilesToProcess" >/dev/null
declare -r FILENAME_TO_REPLACE_1="FileToReplace1.txt"
declare -r OLD_CONTENT_TO_REPLACE_1="OldContent1"
declare -r NEW_CONTENT_TO_REPLACE_1="NewContent1"
declare -r FILENAME_TO_REPLACE_2="FileToReplace2.txt"
declare -r OLD_CONTENT_TO_REPLACE_2="OldContent2"
declare -r NEW_CONTENT_TO_REPLACE_2="NewContent2"
mkdir "Test1"
pushd "Test1" >/dev/null
create_test_subdir "Sub1" "SomeOtherFile2.txt" "SomeOtherFile2 contents."
echo "Not actually a zip file." >"Sub1/NotActuallyAZipFile.zip"
create_test_subdir "Sub2" "$FILENAME_TO_REPLACE_1" "$OLD_CONTENT_TO_REPLACE_1"
# Make the .zip extension uppercase.
mv "Sub2/Sub2-data.zip" "Sub2/Sub2-data.ZIP"
create_test_subdir "Sub3" "$FILENAME_TO_REPLACE_2" "$OLD_CONTENT_TO_REPLACE_2"
popd >/dev/null
mkdir "Test2"
pushd "Test2" >/dev/null
create_test_subdir "Sub1" "SomeOtherFile2.txt" "SomeOtherFile2 contents."
create_test_subdir "Sub2" "SomeOtherFile2.txt" "SomeOtherFile2 contents."
create_test_subdir "Sub3" "$FILENAME_TO_REPLACE_1" "$NEW_CONTENT_TO_REPLACE_1"
create_test_subdir "Sub4" "$FILENAME_TO_REPLACE_2" "$NEW_CONTENT_TO_REPLACE_2"
popd >/dev/null
popd >/dev/null
mkdir "ReplacementFiles"
pushd "ReplacementFiles" >/dev/null
echo "$NEW_CONTENT_TO_REPLACE_1" >"$FILENAME_TO_REPLACE_1"
echo "$NEW_CONTENT_TO_REPLACE_2" >"$FILENAME_TO_REPLACE_2"
popd >/dev/null
popd >/dev/null