Skip to content

Commit 68b87f7

Browse files
Merge pull request #3649 from christiantjl:runtime_tests
PiperOrigin-RevId: 326036575
2 parents a5c2577 + fa31b71 commit 68b87f7

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
{
22
"name": "closure-compiler",
3+
"config": {
4+
"runtime_tests_dir": "./test/com/google/javascript/jscomp/runtime_tests"
5+
},
36
"version": "1.0.0",
47
"repository": "[email protected]:google/closure-compiler.git",
58
"license": "Apache-2",
69
"private": true,
710
"devDependencies": {
811
"closure-compiler-npm": "google/closure-compiler-npm"
12+
},
13+
"dependencies": {
14+
"chalk": "^4.1.0",
15+
"future-event": "^1.5.0",
16+
"glob": "^7.1.6",
17+
"google-closure-library": "^20200719.0.0",
18+
"jest": "^26.1.0",
19+
"jsdom": "^16.3.0"
20+
},
21+
"scripts": {
22+
"build": "clean && mvn -DskipTests",
23+
"build:fast": "mvn -DskipTests -pl externs/pom.xml,pom-main.xml,pom-main-shaded.xml",
24+
"clean": "yarn runtime_tests:clean && mvn clean",
25+
"runtime_tests:build": "$npm_package_config_runtime_tests_dir/utils/build.sh",
26+
"runtime_tests:clean": "rm -rf $npm_package_config_runtime_tests_dir/**/build/*",
27+
"runtime_tests:run": "jest --testRegex $npm_package_config_runtime_tests_dir/utils/test/* --verbose=true",
28+
"runtime_tests": "yarn runtime_tests:clean && yarn run --silent runtime_tests:build && yarn run --silent runtime_tests:run",
29+
"test": "mvn test"
930
}
1031
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
# Copyright 2020 Google Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS-IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
if [ -z $1 ]; then
17+
COMPILATION_LEVEL="SIMPLE"
18+
else
19+
COMPILATION_LEVEL=$1
20+
fi
21+
22+
# This directory.
23+
THIS_DIR=$(dirname $0)
24+
25+
# Location of project root.
26+
PROJECT_ROOT=$(readlink -f $THIS_DIR/../../../../../../..)
27+
28+
# Get the location of the local compiler in this directory, if it exists.
29+
# If it doesn't, build it, then resume execution.
30+
LOCAL_COMPILER="$PROJECT_ROOT/target/closure-compiler-1.0-SNAPSHOT.jar"
31+
if [ ! -f "$LOCAL_COMPILER" ]; then
32+
echo -e "\nCompiler JAR not built. Building...\n" && yarn build:fast
33+
fi
34+
35+
# Build tests from the $TEST_DIR directory, where files like
36+
# `array_pattern_test.js` are stored.
37+
echo -e "\nBuilding runtime tests..."
38+
TEST_DIR="$THIS_DIR/.."
39+
40+
# Get the absolute path of the test directory.
41+
ABS_PATH=$(readlink -f $TEST_DIR)
42+
43+
compileRuntimeTests(){
44+
local -i i=0
45+
local file
46+
for file in $@; do
47+
48+
# /path/to/file.js -> /path/to/file
49+
local file_base=$(echo $file | rev | cut -f 2- -d '.' | rev)
50+
# /path/to/file -> file
51+
local test_name=$(basename $file_base)
52+
# /path/to/file.ext -> /path/to
53+
local test_loc=$(dirname $file)
54+
55+
# Make sure the build directory exists.
56+
mkdir -p $test_loc/build
57+
58+
# Echo a percentage progress indicator.
59+
((i += 1))
60+
echo " $((100 * $i / $#))% | $test_name"
61+
62+
# Output the test file, which will be executed in JSDOM.
63+
cat > $test_loc/build/$test_name.html << EOF
64+
<html>
65+
<head>
66+
<title>$test_name</title>
67+
<script defer>
68+
$(
69+
java -server -XX:+TieredCompilation \
70+
-jar $LOCAL_COMPILER \
71+
-O $COMPILATION_LEVEL \
72+
--language_in ES_NEXT \
73+
--language_out NO_TRANSPILE \
74+
--process_common_js_modules \
75+
--module_resolution NODE \
76+
--dependency_mode PRUNE \
77+
--js $PROJECT_ROOT/node_modules/google-closure-library/ \
78+
--js $ABS_PATH/ \
79+
--entry_point $file
80+
)
81+
</script>
82+
</head>
83+
</html>
84+
EOF
85+
86+
done
87+
}
88+
89+
# build tests
90+
time(
91+
compileRuntimeTests $(find $ABS_PATH -type f -name '*_test.js')
92+
)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* @license MIT
3+
*
4+
* Copyright 2020 The Closure Compiler Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
* use this file except in compliance with the License. You may obtain a copy of
8+
* the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations under
16+
* the License.
17+
*/
18+
/**
19+
* @fileoverview
20+
* Crawl over all ../*_test.html files and execute them in a JSDOM context,
21+
* fail the test if any do not succeed.
22+
*/
23+
24+
const { JSDOM, VirtualConsole } = require('jsdom');
25+
const { fail } = require('assert');
26+
const chalk = require('chalk');
27+
const fs = require('fs');
28+
const FutureEvent = require('future-event');
29+
const glob = require('glob');
30+
const path = require('path');
31+
32+
/**
33+
* The absolute path of test.com.google.javascript.jscomp.runtime_tests.
34+
*/
35+
const RUNTIME_DIR = path.resolve(__dirname, '../..');
36+
37+
/**
38+
* All test files in the test.com.google.javascript.jscomp.runtime_tests.build
39+
* directory.
40+
*/
41+
const TEST_FILES = glob.sync(
42+
`${RUNTIME_DIR}/**/build/*_test.html`,
43+
);
44+
45+
/**
46+
* Iterate over all found test files and execute them in JSDOM.
47+
*/
48+
describe('Runtime tests', () => {
49+
for (const testFile of TEST_FILES) {
50+
const logs = [];
51+
const passed = /PASSED/i;
52+
const failed = /FAILED/i;
53+
54+
const allLogs = () => logs.join('\n');
55+
const chalkMsg = (msg) => {
56+
/**
57+
* Check whether or not this message is a PASSED or FAILED message.
58+
*/
59+
const isPass = passed.test(msg);
60+
const isFail = failed.test(msg);
61+
62+
/**
63+
* Highlight PASSED and FAILED in messages to help with accessibility.
64+
*/
65+
return !(isPass || isFail)
66+
? msg
67+
: msg
68+
.replace(passed, chalk.green('PASSED'))
69+
.replace(failed, chalk.red('FAILED'));
70+
};
71+
72+
/**
73+
* Get filename, i.e. /path/to/file.ext -> file.ext
74+
*/
75+
const testName = path.basename(testFile);
76+
77+
/**
78+
* A promise that will resolve when JSDOM is done executing.
79+
*/
80+
const testIsFinished = new FutureEvent();
81+
82+
/**
83+
* A virtual console which will receive messages from JSDOM's `console.log`.
84+
*/
85+
const virtualConsole = new VirtualConsole()
86+
.on('log', (msg) => {
87+
logs.push(chalkMsg(msg));
88+
if (/Tests complete/i.test(msg)) testIsFinished.ready(allLogs());
89+
else if (/Tests failed/i.test(msg)) testIsFinished.cancel(allLogs());
90+
});
91+
92+
/**
93+
* Load the generated test file for consumption by the JSDOM environment.
94+
* This will be a raw HTML document.
95+
*/
96+
const testDocument = fs.readFileSync(
97+
path.resolve(testFile),
98+
'utf-8',
99+
);
100+
101+
it(`should pass test suite ${path.basename(testFile)}`, async () => {
102+
new JSDOM(testDocument, {
103+
/**
104+
* This does not actually run a server of any kind, it only informs the
105+
* DOM what to put in `window.location.origin`. By default, this is
106+
* `null`, which throws an "unsafe URL" error in the test suite. This is
107+
* purely for accurately mocking a browser for `goog.testing.testsuite`
108+
* tests, and any valid HTTPS URL will work here.
109+
*/
110+
url: 'https://localhost:42',
111+
/**
112+
* This flag will allow the execution of `<script>` tags that are added
113+
* to the DOM after the `onload` event, i.e., those that are added by
114+
* `goog.module` and `goog.require`.
115+
*/
116+
runScripts: 'dangerously',
117+
/**
118+
* Pipe `console.log` to our virtual console.
119+
*/
120+
virtualConsole,
121+
});
122+
123+
try {
124+
/**
125+
* Wait for test to finish, resume if no errors thrown.
126+
*/
127+
await testIsFinished;
128+
} catch (e) {
129+
/**
130+
* Print error and fail if any occurred.
131+
*/
132+
fail(`Failed test in suite ${testName}: \n${e}\n`);
133+
}
134+
/**
135+
* Otherwise, everything passed.
136+
*/
137+
console.log(`Passed all tests in suite ${testName}`);
138+
});
139+
}
140+
});

travis_util/deploy_snapshot.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
#!/bin/bash
2+
# Copyright 2020 Google Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS-IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
215

316
# see https://coderwall.com/p/9b_lfq
417

travis_util/test_npm.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
#!/bin/bash
2+
# Copyright 2020 Google Inc. All Rights Reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS-IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Set build dir to current working dir if not set by Travis, just so it's easy
17+
# to locally run this script.
18+
if [ -z $TRAVIS_BUILD_DIR ]; then
19+
TRAVIS_BUILD_DIR=`pwd`
20+
fi
221

322
# Run yarn install so that dev dependencies are available
423
yarn install && cd ${TRAVIS_BUILD_DIR}/node_modules/closure-compiler-npm && yarn install

0 commit comments

Comments
 (0)