Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 9377a11

Browse files
ofrobotsmatthewloring
authored andcommitted
disableable assertions (#272)
state.js needs some assertions because it interfaces with parts of V8 debug API that are volatile across versions. However, we don't want to be running these assertions in real user code. debug-assert module allows us to run with assertions enabled in the CI. PR-URL: #272
1 parent d3994f8 commit 9377a11

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ install:
1212
- ps: Install-Product node $env:nodejs_version
1313
# install modules
1414
- npm install
15-
# set GCLOUD_PROJECT
1615
- SET GCLOUD_PROJECT=0
16+
- SET CLOUD_DEBUG_ASSERTIONS=1
1717

1818
# Post-install test scripts.
1919
test_script:

bin/run-test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
# Usage: -c to report coverage
44

5+
# Enable assertions
6+
export CLOUD_DEBUG_ASSERTIONS=1
7+
58
while true; do
69
case $1 in
710
-c)

src/agent/debug-assert.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2017 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+
'use strict';
17+
18+
const realAssert = require('assert');
19+
20+
const nop = _=>_;
21+
const fakeAssert = nop;
22+
fakeAssert.deepEqual = fakeAssert.deepStrictEqual = fakeAssert.doesNotThrow =
23+
fakeAssert.equal = fakeAssert.fail = fakeAssert.ifError =
24+
fakeAssert.notDeepEqual = fakeAssert.notDeepStrictEqual =
25+
fakeAssert.notEqual = fakeAssert.notStrictEqual = fakeAssert.ok =
26+
fakeAssert.strictEqual = fakeAssert.throws =
27+
fakeAssert.AssertionError = nop;
28+
29+
module.exports = function debugAssert(enableAssertions) {
30+
return enableAssertions ? realAssert : fakeAssert;
31+
};

src/agent/state.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
module.exports = {
2020
capture: capture,
21-
evaluate: evaluate
21+
evaluate: evaluate,
22+
testAssert: testAssert
2223
};
2324

2425
var ScopeType = require('vm').runInDebugContext('ScopeType');
25-
var assert = require('assert');
26+
var assert = require('./debug-assert')(process.env.CLOUD_DEBUG_ASSERTIONS);
2627
var util = require('util');
2728
var lodash = require('lodash');
2829
var transform = lodash.transform;
@@ -518,3 +519,8 @@ StateResolver.prototype.resolveMirrorProperty_ = function(isEvaluated, property)
518519
}
519520
return this.resolveVariable_(name, property.value(), isEvaluated);
520521
};
522+
523+
// This function is used by unit tests to make sure assertions are enabled.
524+
function testAssert() {
525+
assert.equal(0, 1);
526+
}

test/test-debug-assert.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2017 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+
'use strict';
17+
18+
const realAssert = require('assert');
19+
20+
describe('debug-assert', () => {
21+
const debugAssert = require('../src/agent/debug-assert.js');
22+
23+
it('should fire assertions when enabled', () => {
24+
realAssert.throws(() => {
25+
const assert = debugAssert(true);
26+
assert.equal(1, 2);
27+
});
28+
});
29+
30+
describe('disabled', () => {
31+
const assert = debugAssert(false);
32+
33+
it('should not fire assertions when disabled', () => {
34+
assert.equal(1, 2);
35+
});
36+
37+
it('should cover the full assert API', () => {
38+
Object.keys(realAssert).forEach((key) => {
39+
realAssert.equal(typeof assert[key], 'function');
40+
});
41+
});
42+
});
43+
});

test/test-state.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2017 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+
'use strict';
17+
18+
const assert = require('assert');
19+
const state = require('../src/agent/state.js');
20+
21+
describe('state', () => {
22+
// Testing of state.js is driven through test-v8debugapi.js. There are
23+
// minimal unit tests here.
24+
25+
it('should have assertions enabled', () => {
26+
// this test makes sure that the necessary environment variables to enable
27+
// asserts are present during testing. Use run-tests.sh, or export
28+
// CLOUD_DEBUG_ASSERTIONS=1 to make sure this test passes.
29+
if (!process.env.CLOUD_DEBUG_ASSERTIONS) {
30+
console.log('This test requires the enviornment variable ' +
31+
'CLOUD_DEBUG_ASSERTIONS to be set in order to pass');
32+
}
33+
assert.throws(() => {
34+
state.testAssert();
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)