Comments (2)
I'll use the will-quit
event for this: https://www.electronjs.org/docs/api/app#event-will-quit
from datasette-app.
I got this working, but then failed to write at test for it.
After wasting a bunch of time trying to figure out why I gave up on the test aspect. Here's the code I wrote (which doesn't work):
diff --git a/main.js b/main.js
index 40cad4d..7308f6d 100644
--- a/main.js
+++ b/main.js
@@ -2,6 +2,13 @@ const { app, Menu, BrowserWindow, dialog } = require("electron");
const path = require("path");
const cp = require("child_process");
+function isMocha() {
+ let global = Function('return this')();
+ return ['afterEach','after','beforeEach','before','describe','it'].every(
+ functionName => global[functionName] instanceof Function
+ );
+}
+
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
@@ -11,22 +18,33 @@ function createWindow() {
}, */
});
// Start Python Datasette process
- let datasette = cp.spawn('datasette', ['--memory', '--port', '8024']);
- datasette.on('error', (err) => {
- console.error('Failed to start datasette');
+ let datasette = cp.spawn("datasette", [
+ "--memory",
+ "--port",
+ "8024",
+ "--version-note",
+ "xyz-for-datasette-app",
+ ]);
+ datasette.on("error", (err) => {
+ console.error("Failed to start datasette");
app.quit();
});
+ app.on("will-quit", () => {
+ console.log("Will quit!")
+ datasette.kill();
+ });
- mainWindow.webContents.on("did-fail-load", function() {
+ // Try to load until server is up and running */
+ mainWindow.webContents.on("did-fail-load", function () {
console.log("did-fail-load");
setTimeout(tryAndLoad, 300);
});
function tryAndLoad() {
- mainWindow.loadURL('http://localhost:8024');
- };
+ mainWindow.loadURL("http://localhost:8024");
+ }
setTimeout(tryAndLoad, 300);
-
+
var menu = Menu.buildFromTemplate([
{
label: "Menu",
@@ -37,7 +55,7 @@ function createWindow() {
dialog.showMessageBox({
type: "info",
title: "Datasette",
- message: cp.execSync("datasette --version").toString()
+ message: cp.execSync("datasette --version").toString(),
});
},
},
@@ -56,6 +74,7 @@ function createWindow() {
}
app.whenReady().then(() => {
+ console.log("Ready!")
createWindow();
app.on("activate", function () {
@@ -69,5 +88,7 @@ app.whenReady().then(() => {
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
- if (process.platform !== "darwin") app.quit();
+ if (process.platform !== "darwin" || isMocha()) {
+ app.quit();
+ }
});
diff --git a/test/spec.js b/test/spec.js
index 940a368..53719eb 100644
--- a/test/spec.js
+++ b/test/spec.js
@@ -1,6 +1,7 @@
const Application = require("spectron").Application;
const assert = require("assert");
const electronPath = require("electron");
+const cp = require("child_process");
const path = require("path");
function sleep(ms) {
@@ -18,9 +19,9 @@ describe("Application launch", function () {
return this.app.start();
});
- afterEach(function () {
+ afterEach(async function () {
if (this.app && this.app.isRunning()) {
- return this.app.stop();
+ await this.app.stop();
}
});
@@ -31,4 +32,13 @@ describe("Application launch", function () {
done();
});
});
+ it("does not leave datasette running", async function () {
+ await sleep(1000);
+ await this.app.stop();
+
+ cp.exec("ps aux", (error, stdout, stderr) => {
+ assert.doesNotMatch(stdout, /xyz-for-datasette-app/);
+ done();
+ });
+ });
});
from datasette-app.
Related Issues (20)
- Publish ("export") to web with authentication and metadata (minimum feature set) HOT 4
- Can't "go back" after choosing view as JSON or CSV and saving/storing queries in underlying SQLite
- Allow for load / open of data from URL where CSV (or JSON?) is compressed
- Allow handling of larger CSV files e.g. CFPB complaints data HOT 1
- Switch to Playwright for automated tests HOT 28
- Failing to render in retina resolution when running on my MacBook Pro (macOS Catalina) HOT 1
- Research faster launch times
- Mechanism for plugins that need a specific DB HOT 3
- "Loading..." hangs on launch after parsing "tag_name" in a plugin HOT 6
- npm test is failing HOT 2
- All sorts of features not working due to bad HTTP requests HOT 9
- London Fire Brigade demo breaks HOT 7
- Import CSV from URL should show errors
- New Playwright tests should exercise key features
- Ability to use templates
- The icon looks blurry in the alt-tab window
- Loading... hangs on launch with packaging.version.InvalidVersion HOT 6
- "About Datasette" button fails to open information window HOT 2
- Unable to load preload script HOT 14
- Global Power Plant database is not found. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datasette-app.