Skip to content

Commit c0d10d0

Browse files
committed
Added relevant bash/npm scripts
1 parent 2ab029e commit c0d10d0

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
"google-closure-library": "^20200719.0.0",
1414
"jest": "^26.1.0",
1515
"jsdom": "^16.3.0"
16+
},
17+
"scripts": {
18+
"call-jest": "jest --testRegex .*/utils/js/.* --verbose=true",
19+
"build:runtime_tests": "./utils/runtime_tests_build.sh",
20+
"run:runtime_tests": "./utils/runtime_tests_run.sh",
21+
"runtime_tests": "npm run build:runtime_tests && npm run run:runtime_tests"
1622
}
1723
}

utils/js/runtimeTests.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @file
3+
* Crawl over all test/com/google/.../runtime_tests/*_test.html files and
4+
* execute them in a JSDOM context, fail the test if any do not succeed.
5+
*/
6+
7+
const { JSDOM, VirtualConsole } = require('jsdom');
8+
const fs = require('fs');
9+
const path = require('path');
10+
const FutureEvent = require('future-event');
11+
const { fail } = require('assert');
12+
const glob = require('glob');
13+
14+
const TEST_FILES = glob.sync(path.resolve(
15+
__dirname,
16+
'../../test/com/google/javascript/jscomp/runtime_tests/**/build/*_test.html'
17+
));
18+
19+
describe('Runtime tests', () => {
20+
for (const TEST_URL of TEST_FILES) {
21+
const TEST_NAME = path.basename(TEST_URL);
22+
const logs = [];
23+
const logAll = () => console.log(logs.join('\n'));
24+
const TestIsFinished = new FutureEvent();
25+
const virtualConsole = new VirtualConsole()
26+
.on('log', (msg) => {
27+
logs.push(msg);
28+
if (/Tests complete/i.test(msg)) TestIsFinished.ready();
29+
else if (/Tests failed/i.test(msg)) TestIsFinished.cancel();
30+
});
31+
32+
const TEST_DOC = fs.readFileSync(
33+
path.resolve(
34+
__dirname,
35+
'../../',
36+
TEST_URL
37+
),
38+
'utf-8'
39+
);
40+
41+
it(`should pass test suite ${path.basename(TEST_URL)}`, async () => {
42+
const { window } = new JSDOM(TEST_DOC, {
43+
url: 'https://localhost:42',
44+
runScripts: 'dangerously',
45+
virtualConsole
46+
});
47+
48+
try {
49+
console.log(`Executing tests in suite ${TEST_NAME}`);
50+
await TestIsFinished;
51+
} catch(e) {
52+
logAll();
53+
fail(`Failed test in suite ${TEST_NAME}: \n\n${e}`);
54+
}
55+
56+
});
57+
}
58+
});

utils/runtime_tests_build.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
#!/bin/bash
17+
18+
# to translate from relative dir
19+
abs_dirname() {
20+
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
21+
}
22+
23+
TEST_DIR="test/com/google/javascript/jscomp/runtime_tests"
24+
25+
if [ -z $1 ]; then
26+
ABS_PATH=$(abs_dirname "./$TEST_DIR")
27+
else
28+
ABS_PATH=$(abs_dirname "$1")
29+
fi
30+
31+
i=0
32+
compileRuntimeTests(){
33+
for FILE in $@; do
34+
35+
FILE_BASE=$(echo $FILE | rev | cut -f 2- -d '.' | rev)
36+
TEST_NAME=$(basename $FILE_BASE)
37+
TEST_LOC=$(dirname $FILE)
38+
39+
# make build dir
40+
mkdir -p $TEST_LOC/build
41+
42+
((i += 1))
43+
echo " $((100 * $i / $#))% | $TEST_NAME"
44+
45+
echo "
46+
<html>
47+
<head>
48+
<title>$TEST_NAME</title>
49+
<script defer>
50+
$(google-closure-compiler \
51+
--language_in ES_NEXT \
52+
--language_out NO_TRANSPILE \
53+
--process_common_js_modules \
54+
--module_resolution NODE \
55+
--dependency_mode PRUNE \
56+
--js node_modules/google-closure-library/ \
57+
--js $ABS_PATH/ \
58+
--entry_point $FILE)
59+
</script>
60+
</head>
61+
</html>" > $TEST_LOC/build/$TEST_NAME.html
62+
63+
done
64+
}
65+
66+
# build tests
67+
compileRuntimeTests $(find $ABS_PATH -type f -name '*_test.js')

utils/runtime_tests_run.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
echo "Executing runtime tests..."
17+
npm run --silent call-jest

0 commit comments

Comments
 (0)