Skip to content

Commit

Permalink
ci: fix tauri tests not running in github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Oct 15, 2023
1 parent 598f850 commit 6fbf6a9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tauri-linux-test-pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
sudo apt install build-essential curl wget file libssl-dev libayatana-appindicator3-dev
sudo apt-get install xvfb
- name: build phoenix dist-test
Expand Down
38 changes: 28 additions & 10 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@
<meta name="msapplication-TileColor" content="#47484B">
<title>Phoenix Code</title>

<!-- start inline javascript and non module bootstrap scripts. you must add module scripts to the
javascript module section only!!-->
<script>
function _isTestWindow() {
const isTestPhoenixWindow = !!(new window.URLSearchParams(window.location.search || "")).get("testEnvironment");
const isSpecRunnerWindow = window.location.pathname.endsWith("/SpecRunner.html");
return isTestPhoenixWindow || isSpecRunnerWindow;
}
// In Mac !! the window.__TAURI__ object is present in iframes, even if it doesnt works. so we are forced
// to use if(window.parent.window !== window) instead of if(!window.__TAURI__) here.
if(window.parent.window !== window && window.top.window.__TAURI__) {
Expand All @@ -54,26 +61,41 @@
if(window.__TAURI__) {
function setupTauriBootVars() {
const appNamePromise = window.__TAURI__.app.getName();
const documentDirPromise = window.__TAURI__.path.documentDir();
// for running tests, the user document dir is set to app data dir as we dont want to
// corrupt user documents dir for tests
let documentDirPromise;
if(_isTestWindow()){
documentDirPromise = window.__TAURI__.path.appLocalDataDir(); // appdata/testDocuments will be appended below
} else {
documentDirPromise = window.__TAURI__.path.documentDir();
}

const appLocalDirPromise = window.__TAURI__.path.appLocalDataDir();
const tempDirPromise = window.__TAURI__.os.tempdir();
window._tauriBootVars = {};
window._tauriBootVarsPromise = Promise.all([appNamePromise, documentDirPromise,
appLocalDirPromise, tempDirPromise])
.then((results) => {
window._tauriBootVars.appname = results[0];

window._tauriBootVars.documentDir = results[1];
// For tests, documents dir is localAppDataDir/testDocuments to keep user documents garbage free for tests
// Also In github actions, the tauri get doc dir call gets stuck indefinitely
if(_isTestWindow()){
if(!window._tauriBootVars.documentDir.endsWith(window.__TAURI__.path.sep)){
window._tauriBootVars.documentDir = window._tauriBootVars.documentDir + window.__TAURI__.path.sep;
}
window._tauriBootVars.documentDir = `${window._tauriBootVars.documentDir}testDocuments${window.__TAURI__.path.sep}`;
}
//Documents dir special case for tests

window._tauriBootVars.appLocalDir = results[2];
window._tauriBootVars.tempDir = results[3];
});
}
setupTauriBootVars();
}
</script>

<!-- start inline javascript and non module bootstrap scripts. you must add module scripts to the
javascript module section only!!-->
<script>
// environment setup for boot. do not move out of index html!!
(function(){
function _mobileCheck() {
Expand Down Expand Up @@ -152,11 +174,7 @@
}
return base;
}
function _isTestWindow() {
const isTestPhoenixWindow = !!(new window.URLSearchParams(window.location.search || "")).get("testEnvironment");
const isSpecRunnerWindow = window.location.pathname.endsWith("/SpecRunner.html");
return isTestPhoenixWindow || isSpecRunnerWindow;
}

// Determine OS/platform
let platform = "win";
if (navigator.platform && navigator.platform.match("Mac")) {
Expand Down
11 changes: 10 additions & 1 deletion test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,24 @@
if(window.__TAURI__) {
function setupTauriBootVars() {
const appNamePromise = window.__TAURI__.app.getName();
const documentDirPromise = window.__TAURI__.path.documentDir();
// for running tests, the user document dir is set to app data dir as we dont want to
// corrupt user documents dir for tests
const documentDirPromise = window.__TAURI__.path.appLocalDataDir();
const appLocalDirPromise = window.__TAURI__.path.appLocalDataDir();
const tempDirPromise = window.__TAURI__.os.tempdir();
window._tauriBootVars = {};
window._tauriBootVarsPromise = Promise.all([appNamePromise, documentDirPromise,
appLocalDirPromise, tempDirPromise])
.then((results) => {
window._tauriBootVars.appname = results[0];
// For tests, documents dir is localAppDataDir/documents to keep user documents garbage free for tests
// Also In github actions, the tauri get doc dir call gets stuck indefinitely
window._tauriBootVars.documentDir = results[1];
if(!window._tauriBootVars.documentDir.endsWith(window.__TAURI__.path.sep)){
window._tauriBootVars.documentDir = window._tauriBootVars.documentDir + window.__TAURI__.path.sep;
}
window._tauriBootVars.documentDir = `${window._tauriBootVars.documentDir}documents${window.__TAURI__.path.sep}`;
//Documents dir special case for tests
window._tauriBootVars.appLocalDir = results[2];
window._tauriBootVars.tempDir = results[3];
});
Expand Down
13 changes: 8 additions & 5 deletions test/SpecRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*
*/

/*global beforeEach, afterEach, beforeAll, afterAll, jasmine, Filer, Phoenix */
/*global beforeEach, afterEach, beforeAll, afterAll, jasmine, Filer, Phoenix,
globalTestRunnerLogToConsole, globalTestRunnerErrorToConsole */

// Set the baseUrl to brackets/src
require.config({
Expand Down Expand Up @@ -516,30 +517,32 @@ define(function (require, exports, module) {
}

async function setupAndRunTests() {
globalTestRunnerLogToConsole("Starting tests...");
await window._tauriBootVarsPromise;
let shouldExtract = localStorage.getItem(EXTRACT_TEST_ASSETS_KEY);
if(shouldExtract === EXTRACT || shouldExtract === null) {
_showLoading(true);
let JSZip = require("thirdparty/jszip");
globalTestRunnerLogToConsole("Extracting Test Files");
window.JSZipUtils.getBinaryContent('test_folders.zip', function(err, data) {
if(err) {
alert("Please run 'npm run build' before starting this test. " +
globalTestRunnerErrorToConsole("Please run 'npm run build' before starting this test. " +
"Could not create test files in phoenix virtual fs. Some tests may fail");
_showLoading(false);
init();
} else {
JSZip.loadAsync(data).then(function (zip) {
let keys = Object.keys(zip.files);
let destPath = `/test/`;
console.log("Cleaning test directory: /test/");
globalTestRunnerLogToConsole("Cleaning test directory: /test/");
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
}
console.log("Creating test folder /test/");
globalTestRunnerLogToConsole("Creating test folder /test/");
await makeTestDir();
console.log("Copying test assets to /test/", err);
globalTestRunnerLogToConsole("Copying test assets to /test/", err);
let progressMessageEl = document.getElementById("loadProgressMessage");
for (let i = 0; i < keys.length; i++) {
let path = keys[i];
Expand Down
5 changes: 4 additions & 1 deletion test/spec/Tauri-platform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ define(function (require, exports, module) {
}

it("Should not be able to fetch files in documents folder", async function () {
await testAssetNotAccessibleFolder(await window.__TAURI__.path.documentDir());
// unfortunately for tests, this is set to appdata/testDocuments.
// we cant set this to await window.__TAURI__.path.documentDir() as in github actions,
// the user documents directory is not defined in rust and throws.
await testAssetNotAccessibleFolder(window._tauriBootVars.documentDir);
});

it("Should not be able to fetch files in appLocalData folder", async function () {
Expand Down

0 comments on commit 6fbf6a9

Please sign in to comment.