[Pkg-javascript-commits] [node-tap] 51/186: snapshot: save without snapping removes file

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 16:40:42 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-tap.

commit d4b9d88f46c8bd27d2b29cf31b2a18aff9085a77
Author: isaacs <i at izs.me>
Date:   Tue Oct 31 11:44:06 2017 -0700

    snapshot: save without snapping removes file
---
 lib/snapshot.js  | 15 ++++++++++-----
 unit/snapshot.js |  5 +++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/snapshot.js b/lib/snapshot.js
index c3455f6..40df2ac 100644
--- a/lib/snapshot.js
+++ b/lib/snapshot.js
@@ -4,6 +4,7 @@ const writeFile = require('write-file-atomic')
 const fs = require('fs')
 const mkdirp = require('mkdirp')
 const path = require('path')
+const rimraf = require('rimraf')
 
 class Snapshot {
   constructor (test) {
@@ -42,11 +43,15 @@ class Snapshot {
   }
 
   save () {
-    const data = `'use strict'\n` + (
-      Object.keys(this.snapshot).map(s =>
-        `exports[\`${s}\`] = \`${this.snapshot[s]}\`\n`).join('\n'))
-    mkdirp.sync(path.dirname(this.file))
-    writeFile.sync(this.file, data, 'utf8')
+    if (!this.snapshot)
+      rimraf.sync(this.file)
+    else {
+      const data = `'use strict'\n` + (
+        Object.keys(this.snapshot).map(s =>
+          `exports[\`${s}\`] = \`${this.snapshot[s]}\`\n`).join('\n'))
+      mkdirp.sync(path.dirname(this.file))
+      writeFile.sync(this.file, data, 'utf8')
+    }
   }
 }
 
diff --git a/unit/snapshot.js b/unit/snapshot.js
index 25f1cae..f582a45 100644
--- a/unit/snapshot.js
+++ b/unit/snapshot.js
@@ -34,6 +34,11 @@ t.test('actual test', t => {
   t.ok(ss.match(fs.readFileSync(__filename, 'utf8'), 'this file'))
   t.ok(ss.match('this is fine', 'a statement of acceptance'))
 
+  t.comment('saving without snapping anything removes the file')
+  const sss = new Snapshot(t)
+  sss.save()
+  t.throws(_ => fs.statSync(sss.file), 'file is gone')
+
   t.end()
 })
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-tap.git



More information about the Pkg-javascript-commits mailing list