Coder Social home page Coder Social logo

Comments (2)

simonw avatar simonw commented on May 28, 2024

I'll use the will-quit event for this: https://www.electronjs.org/docs/api/app#event-will-quit

from datasette-app.

simonw avatar simonw commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.