aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/bazel/generate_workspace_test.sh
blob: 584f3e04d84fdd7c223590e1080f1c1daa80705b (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# 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.
#
# Test //external mechanisms
#

# Load test environment
src_dir=$(cd "$(dirname ${BASH_SOURCE[0]})" && pwd)
source $src_dir/test-setup.sh \
  || { echo "test-setup.sh not found!" >&2; exit 1; }
source $src_dir/remote_helpers.sh \
  || { echo "remote_helpers.sh not found!" >&2; exit 1; }

function set_up() {
  # Set up custom repository directory.
  m2=$TEST_TMPDIR/my-m2
  rm -rf $m2
  mkdir -p $m2
  startup_server $m2
}

function tear_down() {
  shutdown_server
  rm -rf $m2
}

function generate_workspace() {
  ${bazel_data}/src/tools/generate_workspace/generate_workspace $@
}

# Takes: groupId, artifactId, and version, extra arguments are dependencies.
function make_artifact() {
  local groupId=$1;
  local artifactId=$2;
  local version=$3;

  shift; shift; shift;

  local pkg_dir=${m2}/${groupId}/${artifactId}/${version}
  local pom_file=${pkg_dir}/${artifactId}-${version}.pom
  mkdir -p ${pkg_dir}
  # Make the pom.xml.
  cat > ${pom_file} <<EOF
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>${groupId}</groupId>
  <artifactId>${artifactId}</artifactId>
  <version>${version}</version>
EOF

  if [[ ${#@} > 0 ]]; then
    echo '  <dependencies>' >> ${pom_file}

    for artifact in $@; do
      IFS=':' read -r -a dep <<< "$artifact"
      cat >> ${pom_file} << EOF
    <dependency>
      <groupId>${dep[0]}</groupId>
      <artifactId>${dep[1]}</artifactId>
      <version>${dep[2]}</version>
    </dependency>
EOF
    done

    echo '  </dependencies>' >> ${pom_file}
  fi

  echo "</project>" >> ${pom_file}

  # Make the jar with one class (we use the groupId for the classname).
  cat > $TEST_TMPDIR/$groupId.java <<EOF
public class $groupId {
  public static void print() {
    System.out.println("$artifactId");
  }
}
EOF
  local jar_path=$pkg_dir/$artifactId-$version.jar
  ${bazel_javabase}/bin/javac $TEST_TMPDIR/$groupId.java
  ${bazel_javabase}/bin/jar cf $jar_path $TEST_TMPDIR/$groupId.class

  local sha1=$(shasum $jar_path | awk '{print $1}')
  echo -n $sha1 > $jar_path.sha1
  echo $sha1
}

function get_workspace_file() {
  cat $TEST_log | tail -n 2 | head -n 1
}

function get_build_file() {
  cat $TEST_log | tail -n 1
}

function test_pom() {
  # Create a maven repo
  local sha1=$(make_artifact blorp glorp 1.2.3)

  # Create a pom that references the artifacts.
  cat > $TEST_TMPDIR/pom.xml <<EOF
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my</groupId>
  <artifactId>thing</artifactId>
  <version>1.0</version>
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>a custom repo</name>
      <url>http://localhost:$fileserver_port/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>blorp</groupId>
      <artifactId>glorp</artifactId>
      <version>1.2.3</version>
    </dependency>
  </dependencies>
</project>
EOF

  generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \
    || fail "generating workspace failed"

  cat $(cat $TEST_log | tail -n 2 | head -n 1) > ws
  cat $(cat $TEST_log | tail -n 1) > build

  assert_contains "artifact = \"blorp:glorp:1.2.3\"," ws
  assert_contains "repository = \"http://localhost:$fileserver_port/\"," ws
  assert_contains "sha1 = \"$sha1\"," ws
  assert_contains "\"@blorp_glorp//jar\"," build
  assert_contains "name = \"blorp_glorp\"," build
}

function test_pom_exclusions() {
  # Create a maven repo
  local sha1_guppy=$(make_artifact fish guppy 2.0)
  local sha1_trout=$(make_artifact fish trout 4.2)

  local sha1_glorp=$(make_artifact blorp glorp 1.2.3 fish:guppy:2.0)
  local sha1_mlorp=$(make_artifact blorp mlorp 3.2.1 fish:trout:4.2)

  # Create a pom that references the artifacts.
  cat > $TEST_TMPDIR/pom.xml <<EOF
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my</groupId>
  <artifactId>thing</artifactId>
  <version>1.0</version>
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>a custom repo</name>
      <url>http://localhost:$fileserver_port/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>blorp</groupId>
      <artifactId>glorp</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>blorp</groupId>
      <artifactId>mlorp</artifactId>
      <version>3.2.1</version>
      <exclusions>
        <exclusion>
          <groupId>fish</groupId>
          <artifactId>trout</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>
EOF

  generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \
    || fail "generating workspace failed"

  cat $(cat $TEST_log | tail -n 2 | head -n 1) > ws
  cat $(cat $TEST_log | tail -n 1) > build

  assert_contains "artifact = \"blorp:glorp:1.2.3\"," ws
  assert_contains "repository = \"http://localhost:$fileserver_port/\"," ws
  assert_contains "sha1 = \"$sha1_glorp\"," ws
  assert_contains "sha1 = \"$sha1_mlorp\"," ws
  assert_contains "sha1 = \"$sha1_guppy\"," ws
  assert_not_contains "sha1 = \"$sha1_trout\"," ws
  assert_contains "\"@blorp_glorp//jar\"," build
  assert_contains "\"@blorp_mlorp//jar\"," build
  assert_contains "name = \"blorp_glorp\"," build
  assert_contains "name = \"blorp_mlorp\"," build
  assert_contains "\"@fish_guppy//jar\"," build
  assert_not_contains "\"@fish_trout//jar\"," build
}

function test_invalid_pom() {
  # No pom file.
  rm -f $TEST_TMPDIR/pom.xml
  generate_workspace -m $TEST_TMPDIR &> $TEST_log
  expect_log "Non-readable POM $TEST_TMPDIR/pom.xml"

  # Invalid XML.
  cat > $TEST_TMPDIR/pom.xml <<EOF
<project>
EOF
  generate_workspace -m $TEST_TMPDIR &> $TEST_log
  expect_log "expected end tag </project>"
}

function test_profile() {
  cat > $TEST_TMPDIR/pom.xml <<EOF
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my</groupId>
  <artifactId>thing</artifactId>
  <version>1.0</version>
  <profiles>
    <profile>
      <id>my-profile</id>
      <activation>
        <property>
          <name>makeThing</name>
          <value>thing</value>
        </property>
      </activation>
    </profile>
  </profiles>
</project>
EOF

  generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \
    || fail "generating workspace failed"
}

function test_submodules() {
  cat > $TEST_TMPDIR/pom.xml <<EOF
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz</groupId>
    <artifactId>a</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <modules>
        <module>b1</module>
        <module>b2</module>
    </modules>
</project>
EOF

  # Create submodules, version and group are inherited from parent.
  mkdir -p $TEST_TMPDIR/{b1,b2}
  cat > $TEST_TMPDIR/b1/pom.xml <<EOF
<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>b1</artifactId>
    <parent>
        <groupId>xyz</groupId>
        <artifactId>a</artifactId>
        <version>1.0</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>xyz</groupId>
            <artifactId>b2</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>
EOF

  cat > $TEST_TMPDIR/b2/pom.xml <<EOF
<project>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>b2</artifactId>
    <parent>
        <groupId>xyz</groupId>
        <artifactId>a</artifactId>
        <version>1.0</version>
    </parent>
</project>
EOF

  generate_workspace -m $TEST_TMPDIR/b1 &> $TEST_log || fail "generate failed"
  expect_log "xyz_b2 was defined in $TEST_TMPDIR/b2/pom.xml which isn't a repository URL"
  assert_contains "artifact = \"xyz:b2:1.0\"," $(get_workspace_file)
}

run_suite "maven tests"