Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Clean up temporary files when running specs
Browse files Browse the repository at this point in the history
  • Loading branch information
damieng committed Dec 1, 2016
1 parent b4c67cb commit 3fd1dbd
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 33 deletions.
9 changes: 6 additions & 3 deletions spec/atom-environment-spec.coffee
@@ -1,10 +1,13 @@
_ = require 'underscore-plus'
path = require 'path'
temp = require 'temp'
temp = require('temp').track()
AtomEnvironment = require '../src/atom-environment'
StorageFolder = require '../src/storage-folder'

describe "AtomEnvironment", ->
afterEach ->
temp.cleanupSync()

describe 'window sizing methods', ->
describe '::getPosition and ::setPosition', ->
originalPosition = null
Expand Down Expand Up @@ -324,7 +327,7 @@ describe "AtomEnvironment", ->

describe "::unloadEditorWindow()", ->
it "saves the BlobStore so it can be loaded after reload", ->
configDirPath = temp.mkdirSync()
configDirPath = temp.mkdirSync('atom-spec-environment')
fakeBlobStore = jasmine.createSpyObj("blob store", ["save"])
atomEnvironment = new AtomEnvironment({applicationDelegate: atom.applicationDelegate, enablePersistence: true, configDirPath, blobStore: fakeBlobStore, window, document})

Expand All @@ -336,7 +339,7 @@ describe "AtomEnvironment", ->

describe "::destroy()", ->
it "does not throw exceptions when unsubscribing from ipc events (regression)", ->
configDirPath = temp.mkdirSync()
configDirPath = temp.mkdirSync('atom-spec-environment')
fakeDocument = {
addEventListener: ->
removeEventListener: ->
Expand Down
1 change: 1 addition & 0 deletions spec/babel-spec.coffee
Expand Up @@ -19,6 +19,7 @@ describe "Babel transpiler support", ->

afterEach ->
CompileCache.setCacheDirectory(originalCacheDir)
temp.cleanupSync()

describe 'when a .js file starts with /** @babel */;', ->
it "transpiles it using babel", ->
Expand Down
5 changes: 4 additions & 1 deletion spec/command-installer-spec.coffee
@@ -1,6 +1,6 @@
path = require 'path'
fs = require 'fs-plus'
temp = require 'temp'
temp = require('temp').track()
CommandInstaller = require '../src/command-installer'

describe "CommandInstaller on #darwin", ->
Expand All @@ -20,6 +20,9 @@ describe "CommandInstaller on #darwin", ->
spyOn(CommandInstaller::, 'getResourcesDirectory').andReturn(resourcesPath)
spyOn(CommandInstaller::, 'getInstallDirectory').andReturn(installationPath)

afterEach ->
temp.cleanupSync()

it "shows an error dialog when installing commands interactively fails", ->
appDelegate = jasmine.createSpyObj("appDelegate", ["confirm"])
installer = new CommandInstaller("2.0.2", appDelegate)
Expand Down
2 changes: 2 additions & 0 deletions spec/compile-cache-spec.coffee
Expand Up @@ -23,6 +23,7 @@ describe 'CompileCache', ->
afterEach ->
CSON.setCacheDir(CompileCache.getCacheDirectory())
CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME)
temp.cleanupSync()

describe 'addPathToCache(filePath, atomHome)', ->
describe 'when the given file is plain javascript', ->
Expand Down Expand Up @@ -81,6 +82,7 @@ describe 'CompileCache', ->

error = new Error("Oops")
expect(error.stack).toBe 'a-stack-trace'
console.log('stack ' + error.getRawStack())
expect(Array.isArray(error.getRawStack())).toBe true

waits(1)
Expand Down
7 changes: 4 additions & 3 deletions spec/config-spec.coffee
@@ -1,5 +1,5 @@
path = require 'path'
temp = require 'temp'
temp = require('temp').track()
CSON = require 'season'
fs = require 'fs-plus'

Expand All @@ -9,13 +9,14 @@ describe "Config", ->
beforeEach ->
spyOn(atom.config, "load")
spyOn(atom.config, "save")
dotAtomPath = temp.path('dot-atom-dir')
dotAtomPath = temp.path('atom-spec-config')
atom.config.configDirPath = dotAtomPath
atom.config.enablePersistence = true
atom.config.configFilePath = path.join(atom.config.configDirPath, "atom.config.cson")

afterEach ->
atom.config.enablePersistence = false
fs.removeSync(dotAtomPath)

describe ".get(keyPath, {scope, sources, excludeSources})", ->
it "allows a key path's value to be read", ->
Expand Down Expand Up @@ -486,8 +487,8 @@ describe "Config", ->
observeHandler.reset() # clear the initial call
atom.config.set('foo.bar.baz', "value 2")
expect(observeHandler).toHaveBeenCalledWith("value 2")
observeHandler.reset()

observeHandler.reset()
atom.config.set('foo.bar.baz', "value 1")
expect(observeHandler).toHaveBeenCalledWith("value 1")
advanceClock(100) # complete pending save that was requested in ::set
Expand Down
14 changes: 9 additions & 5 deletions spec/default-directory-provider-spec.coffee
@@ -1,20 +1,26 @@
DefaultDirectoryProvider = require '../src/default-directory-provider'
path = require 'path'
fs = require 'fs-plus'
temp = require 'temp'
temp = require('temp').track()

describe "DefaultDirectoryProvider", ->
tmp = null

beforeEach ->
tmp = temp.mkdirSync('atom-spec-default-dir-provider')

afterEach ->
temp.cleanupSync()

describe ".directoryForURISync(uri)", ->
it "returns a Directory with a path that matches the uri", ->
provider = new DefaultDirectoryProvider()
tmp = temp.mkdirSync()

directory = provider.directoryForURISync(tmp)
expect(directory.getPath()).toEqual tmp

it "normalizes its input before creating a Directory for it", ->
provider = new DefaultDirectoryProvider()
tmp = temp.mkdirSync()
nonNormalizedPath = tmp + path.sep + ".." + path.sep + path.basename(tmp)
expect(tmp.includes("..")).toBe false
expect(nonNormalizedPath.includes("..")).toBe true
Expand All @@ -24,7 +30,6 @@ describe "DefaultDirectoryProvider", ->

it "creates a Directory for its parent dir when passed a file", ->
provider = new DefaultDirectoryProvider()
tmp = temp.mkdirSync()
file = path.join(tmp, "example.txt")
fs.writeFileSync(file, "data")

Expand All @@ -40,7 +45,6 @@ describe "DefaultDirectoryProvider", ->
describe ".directoryForURI(uri)", ->
it "returns a Promise that resolves to a Directory with a path that matches the uri", ->
provider = new DefaultDirectoryProvider()
tmp = temp.mkdirSync()

waitsForPromise ->
provider.directoryForURI(tmp).then (directory) ->
Expand Down
7 changes: 5 additions & 2 deletions spec/file-system-blob-store-spec.coffee
@@ -1,4 +1,4 @@
temp = require 'temp'
temp = require('temp').track()
path = require 'path'
fs = require 'fs-plus'
FileSystemBlobStore = require '../src/file-system-blob-store'
Expand All @@ -7,9 +7,12 @@ describe "FileSystemBlobStore", ->
[storageDirectory, blobStore] = []

beforeEach ->
storageDirectory = temp.path()
storageDirectory = temp.path('atom-spec-filesystemblobstore')
blobStore = FileSystemBlobStore.load(storageDirectory)

afterEach ->
fs.removeSync(storageDirectory)

it "is empty when the file doesn't exist", ->
expect(blobStore.get("foo", "invalidation-key-1")).toBeUndefined()
expect(blobStore.get("bar", "invalidation-key-2")).toBeUndefined()
Expand Down
5 changes: 4 additions & 1 deletion spec/git-repository-provider-spec.coffee
@@ -1,6 +1,6 @@
path = require 'path'
fs = require 'fs-plus'
temp = require 'temp'
temp = require('temp').track()
{Directory} = require 'pathwatcher'
GitRepository = require '../src/git-repository'
GitRepositoryProvider = require '../src/git-repository-provider'
Expand All @@ -11,6 +11,9 @@ describe "GitRepositoryProvider", ->
beforeEach ->
provider = new GitRepositoryProvider(atom.project, atom.config, atom.confirm)

afterEach ->
temp.cleanupSync()

describe ".repositoryForDirectory(directory)", ->
describe "when specified a Directory with a Git repository", ->
it "returns a Promise that resolves to a GitRepository", ->
Expand Down
6 changes: 4 additions & 2 deletions spec/git-repository-spec.coffee
@@ -1,11 +1,11 @@
temp = require 'temp'
temp = require('temp').track()
GitRepository = require '../src/git-repository'
fs = require 'fs-plus'
path = require 'path'
Project = require '../src/project'

copyRepository = ->
workingDirPath = temp.mkdirSync('atom-working-dir')
workingDirPath = temp.mkdirSync('atom-spec-git')
fs.copySync(path.join(__dirname, 'fixtures', 'git', 'working-dir'), workingDirPath)
fs.renameSync(path.join(workingDirPath, 'git.git'), path.join(workingDirPath, '.git'))
workingDirPath
Expand All @@ -19,6 +19,8 @@ describe "GitRepository", ->

afterEach ->
repo.destroy() if repo?.repo?
try
temp.cleanupSync() # These tests sometimes lag at shutting down resources

describe "@open(path)", ->
it "returns null when no repository is found", ->
Expand Down
5 changes: 4 additions & 1 deletion spec/grammars-spec.coffee
@@ -1,6 +1,6 @@
path = require 'path'
fs = require 'fs-plus'
temp = require 'temp'
temp = require('temp').track()
GrammarRegistry = require '../src/grammar-registry'
Grim = require 'grim'

Expand All @@ -24,6 +24,7 @@ describe "the `grammars` global", ->
afterEach ->
atom.packages.deactivatePackages()
atom.packages.unloadPackages()
temp.cleanupSync()

describe ".selectGrammar(filePath)", ->
it "always returns a grammar", ->
Expand Down Expand Up @@ -96,6 +97,7 @@ describe "the `grammars` global", ->
)
grammar1 = atom.grammars.loadGrammarSync(grammarPath1)
expect(atom.grammars.selectGrammar('more.test', '')).toBe grammar1
fs.removeSync(grammarPath1)

grammarPath2 = temp.path(suffix: '.json')
fs.writeFileSync grammarPath2, JSON.stringify(
Expand All @@ -105,6 +107,7 @@ describe "the `grammars` global", ->
)
grammar2 = atom.grammars.loadGrammarSync(grammarPath2)
expect(atom.grammars.selectGrammar('more.test', '')).toBe grammar2
fs.removeSync(grammarPath2)

it "favors non-bundled packages when breaking scoring ties", ->
waitsForPromise ->
Expand Down
3 changes: 2 additions & 1 deletion spec/integration/helpers/start-atom.coffee
Expand Up @@ -16,7 +16,7 @@ ChromedriverPort = 9515
ChromedriverURLBase = "/wd/hub"
ChromedriverStatusURL = "http://localhost:#{ChromedriverPort}#{ChromedriverURLBase}/status"

userDataDir = temp.mkdirSync('atom-user-data-dir')
userDataDir = null

chromeDriverUp = (done) ->
checkStatus = ->
Expand All @@ -38,6 +38,7 @@ chromeDriverDown = (done) ->
setTimeout(checkStatus, 100)

buildAtomClient = (args, env) ->
userDataDir = temp.mkdirSync('atom-user-data-dir')
client = webdriverio.remote(
host: 'localhost'
port: ChromedriverPort
Expand Down
18 changes: 16 additions & 2 deletions spec/main-process/file-recovery-service.test.js
Expand Up @@ -2,19 +2,23 @@

import {dialog} from 'electron'
import FileRecoveryService from '../../src/main-process/file-recovery-service'
import temp from 'temp'
import fs from 'fs-plus'
import sinon from 'sinon'
import {escapeRegExp} from 'underscore-plus'
const temp = require('temp').track()

describe("FileRecoveryService", () => {
let recoveryService, recoveryDirectory

beforeEach(() => {
recoveryDirectory = temp.mkdirSync()
recoveryDirectory = temp.mkdirSync('atom-spec-file-recovery')
recoveryService = new FileRecoveryService(recoveryDirectory)
})

afterEach(() => {
temp.cleanupSync()
})

describe("when no crash happens during a save", () => {
it("creates a recovery file and deletes it after saving", () => {
const mockWindow = {}
Expand All @@ -28,6 +32,8 @@ describe("FileRecoveryService", () => {
recoveryService.didSavePath(mockWindow, filePath)
assert.equal(fs.listTreeSync(recoveryDirectory).length, 0)
assert.equal(fs.readFileSync(filePath, 'utf8'), "changed")

fs.removeSync(filePath)
})

it("creates only one recovery file when many windows attempt to save the same file, deleting it when the last one finishes saving it", () => {
Expand All @@ -48,6 +54,8 @@ describe("FileRecoveryService", () => {
recoveryService.didSavePath(anotherMockWindow, filePath)
assert.equal(fs.listTreeSync(recoveryDirectory).length, 0)
assert.equal(fs.readFileSync(filePath, 'utf8'), "changed")

fs.removeSync(filePath)
})
})

Expand All @@ -64,6 +72,8 @@ describe("FileRecoveryService", () => {
recoveryService.didCrashWindow(mockWindow)
assert.equal(fs.listTreeSync(recoveryDirectory).length, 0)
assert.equal(fs.readFileSync(filePath, 'utf8'), "some content")

fs.removeSync(filePath)
})

it("restores the created recovery file when many windows attempt to save the same file and one of them crashes", () => {
Expand Down Expand Up @@ -94,6 +104,8 @@ describe("FileRecoveryService", () => {
recoveryService.didCrashWindow(anotherMockWindow)
assert.equal(fs.readFileSync(filePath, 'utf8'), "D")
assert.equal(fs.listTreeSync(recoveryDirectory).length, 0)

fs.removeSync(filePath)
})

it("emits a warning when a file can't be recovered", sinon.test(function () {
Expand All @@ -113,6 +125,8 @@ describe("FileRecoveryService", () => {
assert.equal(logs.length, 1)
assert.match(logs[0], new RegExp(escapeRegExp(filePath)))
assert.match(logs[0], new RegExp(escapeRegExp(recoveryFiles[0])))

fs.removeSync(filePath)
}))
})

Expand Down
5 changes: 4 additions & 1 deletion spec/module-cache-spec.coffee
@@ -1,13 +1,16 @@
path = require 'path'
Module = require 'module'
fs = require 'fs-plus'
temp = require 'temp'
temp = require('temp').track()
ModuleCache = require '../src/module-cache'

describe 'ModuleCache', ->
beforeEach ->
spyOn(Module, '_findPath').andCallThrough()

afterEach ->
temp.cleanupSync()

it 'resolves Electron module paths without hitting the filesystem', ->
builtins = ModuleCache.cache.builtins
expect(Object.keys(builtins).length).toBeGreaterThan 0
Expand Down
9 changes: 7 additions & 2 deletions spec/package-manager-spec.coffee
@@ -1,6 +1,6 @@
path = require 'path'
Package = require '../src/package'
temp = require 'temp'
temp = require('temp').track()
fs = require 'fs-plus'
{Disposable} = require 'atom'
{buildKeydownEvent} = require '../src/keymap-extensions'
Expand All @@ -17,6 +17,9 @@ describe "PackageManager", ->
beforeEach ->
workspaceElement = atom.views.getView(atom.workspace)

afterEach ->
temp.cleanupSync()

describe "::getApmPath()", ->
it "returns the path to the apm command", ->
apmPath = path.join(process.resourcesPath, "app", "apm", "bin", "apm")
Expand Down Expand Up @@ -643,7 +646,7 @@ describe "PackageManager", ->
[element, events, userKeymapPath] = []

beforeEach ->
userKeymapPath = path.join(temp.path(), "user-keymaps.cson")
userKeymapPath = path.join(temp.mkdirSync(), "user-keymaps.cson")
spyOn(atom.keymaps, "getUserKeymapPath").andReturn(userKeymapPath)

element = createTestElement('test-1')
Expand All @@ -660,6 +663,8 @@ describe "PackageManager", ->
atom.keymaps.watchSubscriptions[userKeymapPath].dispose()
delete atom.keymaps.watchSubscriptions[userKeymapPath]

temp.cleanupSync()

it "doesn't override user-defined keymaps", ->
fs.writeFileSync userKeymapPath, """
".test-1":
Expand Down

0 comments on commit 3fd1dbd

Please sign in to comment.