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
|
#!/bin/sh
# Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
TOOL=$(realpath ./bin/tool-under-test)
mkdir -p .root
BUILDROOT=$(realpath .root)
mkdir -p out
OUTDIR_BASE=$(realpath out)
echo {} > repos.json
export CONF=$(realpath repos.json)
mkdir src
cd src
touch ROOT
echo -n A > a.txt
chmod 644 a.txt
echo -n B > b.txt
echo -n C > c.txt
chmod 755 b.txt
mkdir d.txt
mkdir foo
echo too deep > foo/a.txt
cat > RULES <<'EOI'
{ "reflect":
{ "target_fields": ["deps"]
, "expression":
{ "type": "let*"
, "bindings":
[ [ "inputs"
, { "type": "map_union"
, "$1":
{ "type": "foreach"
, "var": "x"
, "range": {"type": "FIELD", "name": "deps"}
, "body":
{"type": "DEP_ARTIFACTS", "dep": {"type": "var", "name": "x"}}
}
}
]
, [ "keys"
, { "type": "singleton_map"
, "key": "keys.txt"
, "value":
{ "type": "BLOB"
, "data":
{ "type": "join"
, "separator": " "
, "$1": {"type": "keys", "$1": {"type": "var", "name": "inputs"}}
}
}
}
]
, [ "content"
, { "type": "ACTION"
, "inputs":
{ "type": "to_subdir"
, "subdir": "work"
, "$1": {"type": "var", "name": "inputs"}
}
, "outs": ["content.txt"]
, "cmd":
[ "sh"
, "-c"
, { "type": "join"
, "separator": " "
, "$1":
[ "mkdir -p work && cd work &&"
, { "type": "join_cmd"
, "$1":
{ "type": "++"
, "$1":
[ ["cat"]
, { "type": "keys"
, "$1": {"type": "var", "name": "inputs"}
}
]
}
}
, "> ../content.txt"
]
}
]
}
]
, [ "outputs"
, { "type": "map_union"
, "$1":
[ {"type": "var", "name": "keys"}
, {"type": "var", "name": "content"}
]
}
]
]
, "body":
{"type": "RESULT", "artifacts": {"type": "var", "name": "outputs"}}
}
}
}
EOI
cat > TARGETS <<'EOI'
{ "b.txt": {"type": "file_gen", "name": "b-new.txt", "data": "fromtarget"}
, "enumeration": {"type": "reflect", "deps": ["a.txt", "b.txt", "c.txt"]}
, "glob": {"type": "reflect", "deps": [["GLOB", null, "*.txt"]]}
, "with_target":
{"type": "reflect", "deps": [["GLOB", null, "*.txt"], "b.txt"]}
, "not_top_level": {"type": "reflect", "deps": [["GLOB", null, "foo/*.txt"]]}
}
EOI
do_test() {
echo === Enumeration refres to targets ===
${TOOL} install -C ${CONF} --local-build-root ${BUILDROOT} -o ${OUTDIR}/enum enumeration 2>&1
cat ${OUTDIR}/enum/keys.txt
echo
cat ${OUTDIR}/enum/content.txt
echo
[ "$(cat ${OUTDIR}/enum/keys.txt)" = "a.txt b-new.txt c.txt" ]
[ "$(cat ${OUTDIR}/enum/content.txt)" = "AfromtargetC" ]
echo === Glob always refres to files and directories are ignored ===
${TOOL} install -C ${CONF} --local-build-root ${BUILDROOT} -o ${OUTDIR}/glob glob 2>&1
cat ${OUTDIR}/glob/keys.txt
echo
cat ${OUTDIR}/glob/content.txt
echo
[ "$(cat ${OUTDIR}/glob/keys.txt)" = "a.txt b.txt c.txt" ]
[ "$(cat ${OUTDIR}/glob/content.txt)" = "ABC" ]
echo === Globs and targets can be combined ===
${TOOL} install -C ${CONF} --local-build-root ${BUILDROOT} -o ${OUTDIR}/with_target with_target 2>&1
cat ${OUTDIR}/with_target/keys.txt
echo
cat ${OUTDIR}/with_target/content.txt
echo
[ "$(cat ${OUTDIR}/with_target/keys.txt)" = "a.txt b-new.txt b.txt c.txt" ]
[ "$(cat ${OUTDIR}/with_target/content.txt)" = "AfromtargetBC" ]
echo === Globs only inspect the top-level directory of the module ===
${TOOL} install -C ${CONF} --local-build-root ${BUILDROOT} -o ${OUTDIR}/not_top_level not_top_level 2>&1
cat ${OUTDIR}/not_top_level/keys.txt
echo
cat ${OUTDIR}/not_top_level/content.txt
echo
[ -z "$(cat ${OUTDIR}/not_top_level/keys.txt)" ]
[ -z "$(cat ${OUTDIR}/not_top_level/content.txt)" ]
echo === Done ===
}
echo
echo '***** Tests on the plain file system *****'
echo
OUTDIR=${OUTDIR_BASE}/file_system
do_test
echo
echo '***** Tests on a git root *****'
echo
git init
git add .
git config user.name "N.O.Body"
git config user.email "nobody@example.com"
git commit -m 'Initial commit'
REPO=$(realpath .git)
TREE=$(git log -n1 --format='%T')
cat > $CONF <<EOF
{"repositories": {"": {"workspace_root": ["git tree", "${TREE}", "${REPO}"]}}}
EOF
echo
cat $CONF
echo
OUTDIR=${OUTDIR_BASE}/git
do_test
|