-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0624d1c
commit 8ed3b59
Showing
7 changed files
with
270 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
assert.strictEqual(fs.foo, 'I am from the snapshot'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
fs.foo = 'I am from the snapshot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
// This tests that user land snapshots works when the instance restored from | ||
// the snapshot is launched as a CJS module. | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
tmpdir.refresh(); | ||
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); | ||
const file = fixtures.path('snapshot', 'mutate-fs.js'); | ||
const checkFile = fixtures.path('snapshot', 'check-mutate-fs.js'); | ||
|
||
{ | ||
// Create the snapshot. | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
'--snapshot-blob', | ||
blobPath, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
const stats = fs.statSync(blobPath); | ||
assert(stats.isFile()); | ||
} | ||
|
||
{ | ||
// Run the check file as a CJS module | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-blob', | ||
blobPath, | ||
checkFile, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
|
||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
// This tests the basic functionality of --snapshot-main. | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
tmpdir.refresh(); | ||
const file = fixtures.path('snapshot', 'mutate-fs.js'); | ||
|
||
{ | ||
// By default, the snapshot blob path is snapshot.blob at cwd | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); | ||
assert(stats.isFile()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
// This tests that user land snapshots works when the instance restored from | ||
// the snapshot is launched with --help, --check | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
tmpdir.refresh(); | ||
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); | ||
const file = fixtures.path('snapshot', 'mutate-fs.js'); | ||
|
||
{ | ||
// Create the snapshot. | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
'--snapshot-blob', | ||
blobPath, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
const stats = fs.statSync(blobPath); | ||
assert(stats.isFile()); | ||
} | ||
|
||
{ | ||
// Check --help. | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-blob', | ||
blobPath, | ||
'--help', | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
|
||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
|
||
assert(child.stdout.toString().includes('--help')); | ||
} | ||
|
||
{ | ||
// Check -c. | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-blob', | ||
blobPath, | ||
'-c', | ||
file, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
|
||
// Check that it is a noop. | ||
assert.strictEqual(child.stdout.toString().trim(), ''); | ||
assert.strictEqual(child.stderr.toString().trim(), ''); | ||
assert.strictEqual(child.status, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
// This tests that user land snapshots works when the instance restored from | ||
// the snapshot is launched as a REPL. | ||
|
||
const common = require('../common'); | ||
if (common.isWindows) { | ||
// No way to send CTRL_C_EVENT to processes from JS right now. | ||
common.skip('platform not supported'); | ||
} | ||
const assert = require('assert'); | ||
const { spawnSync, spawn } = require('child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const path = require('path'); | ||
|
||
tmpdir.refresh(); | ||
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); | ||
const file = fixtures.path('snapshot', 'mutate-fs.js'); | ||
|
||
{ | ||
// Create the snapshot. | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
'--snapshot-blob', | ||
blobPath, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
} | ||
|
||
{ | ||
// Use the snapshot to run the REPL. | ||
const child = spawn(process.execPath, [ | ||
'--snapshot-blob', | ||
blobPath, | ||
'-i', | ||
], { | ||
cwd: tmpdir.path, | ||
env: { | ||
...process.env, | ||
REPL_TEST_PPID: process.pid | ||
} | ||
}); | ||
|
||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
|
||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
|
||
child.stdout.once('data', () => { | ||
process.on('SIGUSR2', () => { | ||
process.kill(child.pid, 'SIGINT'); | ||
child.stdout.once('data', () => { | ||
// Make sure state from before the interruption is still available. | ||
child.stdin.end('fs.foo\n'); | ||
}); | ||
}); | ||
|
||
child.stdin.write('process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' + | ||
'while(true){}\n'); | ||
}); | ||
|
||
child.on('close', common.mustCall(function(code) { | ||
if (code !== 0) { | ||
console.log(stderr); | ||
console.log(stdout); | ||
assert.strictEqual(code, 0); | ||
} | ||
// Check that fs.foo is mutated from the snapshot | ||
assert(/I am from the snapshot/.test(stdout)); | ||
})); | ||
|
||
child.stdin.end("require('fs').foo"); | ||
} |