Skip to content

Commit

Permalink
test: point all unit and integ tests folder to tauri node fs in tauri…
Browse files Browse the repository at this point in the history
… env
  • Loading branch information
abose committed Dec 12, 2023
1 parent 37a27f1 commit d94256b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
19 changes: 14 additions & 5 deletions src-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function randomNonce(byteLength) {
return randomId;
}

let debugMode = false;
const COMMAND_RESPONSE_PREFIX = 'phnodeResp:';
// Generate a random 64-bit url. This should take 100 million+ of years to crack with current http connection speed.
const PHOENIX_FS_URL = `/PhoenixFS${randomNonce(8)}`;
Expand All @@ -66,25 +67,33 @@ function _sendResponse(responseMessage, commandID) {
}) + "\n");
}

function resetOrphanExitTimer() {
const timeout = debugMode ? 60000 * 15 : 60000;
// in debug mode, we wait for more minutes to not exit node if phcode doesn't send heartbeats on break point debug
clearTimeout(orphanExitTimer);
orphanExitTimer = setTimeout(()=>{
process.exit(1);
}, timeout);
}

function processCommand(line) {
try{
let jsonCmd = JSON.parse(line);
switch (jsonCmd.commandCode) {
case "terminate": process.exit(0); return;
case "heartBeat":
clearTimeout(orphanExitTimer);
orphanExitTimer = setTimeout(()=>{
process.exit(1);
}, 60000);
resetOrphanExitTimer();
return;
case "ping": _sendResponse("pong", jsonCmd.commandID); return;
case "setDebugMode":
if(jsonCmd.commandData) {
debugMode = jsonCmd.commandData;
if(debugMode) {
console.log = savedConsoleLog;
console.log("Debug Mode Enabled");
} else {
console.log = function () {}; // swallow logs
}
resetOrphanExitTimer();
_sendResponse("done", jsonCmd.commandID); return;
case "getEndpoints":
serverPortPromise.then(port =>{
Expand Down
7 changes: 6 additions & 1 deletion src/node-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ if(Phoenix.browser.isTauri) {
child = await command.spawn();

const execNode = function (commandCode, commandData) {
if(window.isNodeTerminated){
return Promise.reject("Node is terminated! Cannot execute: " + commandCode);
}
const newCommandID = commandID ++;
child.write(JSON.stringify({
commandCode: commandCode, commandID: newCommandID, commandData
Expand All @@ -92,7 +95,9 @@ if(Phoenix.browser.isTauri) {
setInspectEnabled,
isInspectEnabled,
terminateNode: function () {
execNode(NODE_COMMANDS.TERMINATE);
if(!window.isNodeTerminated) {
execNode(NODE_COMMANDS.TERMINATE);
}
},
getInspectPort: function () {
return inspectPort;
Expand Down
1 change: 1 addition & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@

<script src="../src/phoenix/shell.js" type="module"></script>
<script src="virtual-server-loader.js" type="module"></script>
<script src="../src/node-loader.js"></script>

<link href="../src/thirdparty/bootstrap/bootstrap.min.css" rel="stylesheet">
<link href="../src/thirdparty/bootstrap/bootstrap-grid.min.css" rel="stylesheet">
Expand Down
12 changes: 6 additions & 6 deletions test/SpecRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ define(function (require, exports, module) {

function _copyZippedItemToFS(path, item) {
return new Promise((resolve, reject) =>{
let destPath = `/test/${path}`;
let destPath = `${SpecRunnerUtils.getTestPath()}/${path}`;
if(item.dir){
window.fs.mkdirs(destPath, 0o777, true, (err)=>{
if(err){
Expand All @@ -525,7 +525,7 @@ define(function (require, exports, module) {

function makeTestDir() {
return new Promise((resolve, reject)=>{
let testPath = `/test/`;
let testPath = SpecRunnerUtils.getTestPath();
window.fs.mkdirs(testPath, 0o777, true, (err)=>{
if(err){
reject();
Expand Down Expand Up @@ -553,16 +553,16 @@ define(function (require, exports, module) {
} else {
JSZip.loadAsync(data).then(function (zip) {
let keys = Object.keys(zip.files);
let destPath = `/test/`;
globalTestRunnerLogToConsole("Cleaning test directory: /test/");
let destPath = SpecRunnerUtils.getTestPath();
globalTestRunnerLogToConsole("Cleaning test directory: " + destPath);
window.fs.unlink(destPath, async function (err) {
if(err && err.code !== 'ENOENT'){
console.error("Could not clean test dir. we will try to move ahead", err);
// we will now try to overwrite existing
}
globalTestRunnerLogToConsole("Creating test folder /test/");
globalTestRunnerLogToConsole("Creating test folder " + destPath);
await makeTestDir();
globalTestRunnerLogToConsole("Copying test assets to /test/");
globalTestRunnerLogToConsole("Copying test assets to " + destPath);
let progressMessageEl = document.getElementById("loadProgressMessage");
let lastPrintedPercent = 0;
for (let i = 0; i < keys.length; i++) {
Expand Down
12 changes: 6 additions & 6 deletions test/spec/FindInFiles-integ-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,19 @@ define(function (require, exports, module) {
let editor;
await awaitsFor(function () {
editor = SearchResultsView._previewEditorForTests;
return (editor && editor.document.file.fullPath === "/test/spec/FindReplace-test-files/css/foo.css");
}, "keyboard nav", 1000);
return (editor && editor.document.file.fullPath === SpecRunnerUtils.getTestPath("/spec/FindReplace-test-files/css/foo.css"));
}, "file open");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", $searchField[0]);
await awaitsFor(function () {
editor = SearchResultsView._previewEditorForTests;
return (editor && editor.document.file.fullPath === "/test/spec/FindReplace-test-files/foo.js");
}, "keyboard nav", 1000);
return (editor && editor.document.file.fullPath === SpecRunnerUtils.getTestPath("/spec/FindReplace-test-files/foo.js"));
}, "keyboard nav down");
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_UP, "keydown", $searchField[0]);
await awaitsFor(function () {
editor = SearchResultsView._previewEditorForTests;
console.log(editor && editor.document.file.fullPath);
return (editor && editor.document.file.fullPath === "/test/spec/FindReplace-test-files/css/foo.css");
}, "keyboard nav", 1000);
return (editor && editor.document.file.fullPath === SpecRunnerUtils.getTestPath("/spec/FindReplace-test-files/css/foo.css"));
}, "keyboard nav up");
});

it("should find start and end positions", async function () {
Expand Down
8 changes: 4 additions & 4 deletions test/spec/LowLevelFileIO-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ define(function (require, exports, module) {

brackets.fs.readdir("/This/directory/doesnt/exist", cb);

await awaitsFor(function () { return cb.wasCalled; }, "readdir to finish", 1000);
await awaitsFor(function () { return cb.wasCalled; }, "readdir to finish");

expect(cb.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
});
Expand Down Expand Up @@ -200,7 +200,7 @@ define(function (require, exports, module) {

brackets.fs.stat("/This/directory/doesnt/exist", cb);

await awaitsFor(function () { return cb.wasCalled; }, "stat to finish", 1000);
await awaitsFor(function () { return cb.wasCalled; }, "stat to finish");

expect(cb.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
});
Expand Down Expand Up @@ -235,7 +235,7 @@ define(function (require, exports, module) {

brackets.fs.readFile("/This/file/doesnt/exist.txt", UTF8, cb);

await awaitsFor(function () { return cb.wasCalled; }, "readFile to finish", 1000);
await awaitsFor(function () { return cb.wasCalled; }, "readFile to finish");

expect(cb.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
});
Expand Down Expand Up @@ -406,7 +406,7 @@ define(function (require, exports, module) {

brackets.fs.unlink("/This/file/doesnt/exist.txt", cb);

await awaitsFor(function () { return cb.wasCalled; }, "unlink to finish", 1000);
await awaitsFor(function () { return cb.wasCalled; }, "unlink to finish");

expect(cb.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
});
Expand Down
6 changes: 5 additions & 1 deletion test/spec/ProjectManager-integ-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/

/*global describe, it, expect, afterEach, awaitsFor, awaitsForDone, beforeAll, afterAll, awaits */
/*global describe, it, expect, afterEach, awaitsFor, awaitsForDone, beforeAll, afterAll, awaits, Phoenix */

define(function (require, exports, module) {

Expand Down Expand Up @@ -509,6 +509,10 @@ define(function (require, exports, module) {
});

describe("Project, file and folder download", function () {
if(Phoenix.browser.isTauri) {
it("Not tested: download project is not present desktop local file system", async function () {});
return;
}
it("should download project command work", async function () {
let restore = testWindow.saveAs;
let blob, name;
Expand Down
11 changes: 9 additions & 2 deletions test/spec/SpecRunnerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/

/*global jsPromise, jasmine, expect, beforeEach, awaitsFor,awaitsForDone, spyOn, KeyboardEvent, waits, awaits */
/*global Phoenix, jsPromise, jasmine, expect, awaitsFor,awaitsForDone, spyOn, awaits */

define(function (require, exports, module) {

Expand Down Expand Up @@ -216,10 +216,16 @@ define(function (require, exports, module) {
}

function getTestRoot() {
if(Phoenix.browser.isTauri){
return Phoenix.app.getApplicationSupportDirectory() + "test";
}
return '/test';
}

function getTestPath(path) {
function getTestPath(path = '') {
if(path && !path.startsWith("/")){
throw new Error("getTestPath path should start with a /");
}
return getTestRoot() + path;
}

Expand Down Expand Up @@ -712,6 +718,7 @@ define(function (require, exports, module) {
delete _testWindow.fs;

}
_testWindow.PhNodeEngine && _testWindow.PhNodeEngine.terminateNode();
if(blankTestWindow){
_testWindow.location.href = "about:blank";
await awaits(2000); // UTS will crap without these time waits, esp in chromium. Browser freezes
Expand Down

0 comments on commit d94256b

Please sign in to comment.