From 173502bab4340150ce5e57e8515ecaed17fc4c1f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 26 Feb 2015 10:46:39 -0800 Subject: [PATCH] Base state path on project paths --- spec/atom-spec.coffee | 28 ++++++++++++++++++++++++++++ src/atom.coffee | 11 +++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/spec/atom-spec.coffee b/spec/atom-spec.coffee index 2c1a40afc..aa777b134 100644 --- a/spec/atom-spec.coffee +++ b/spec/atom-spec.coffee @@ -3,6 +3,8 @@ Exec = require('child_process').exec path = require 'path' Package = require '../src/package' ThemeManager = require '../src/theme-manager' +_ = require "underscore-plus" +temp = require "temp" describe "the `atom` global", -> describe 'window sizing methods', -> @@ -124,3 +126,29 @@ describe "the `atom` global", -> line: 2 column: 3 originalError: error + + describe "saving and loading", -> + afterEach -> atom.mode = "spec" + + it "selects the state based on the current project paths", -> + Atom = atom.constructor + [dir1, dir2] = [temp.mkdirSync("dir1-"), temp.mkdirSync("dir2-")] + + loadSettings = _.extend Atom.getLoadSettings(), + initialPaths: [dir1] + windowState: null + + spyOn(Atom, 'getLoadSettings').andCallFake -> loadSettings + spyOn(Atom, 'getStorageDirPath').andReturn(temp.mkdirSync("storage-dir-")) + + atom.mode = "editor" + atom.state.stuff = "cool" + atom.project.setPaths([dir1, dir2]) + atom.saveSync.originalValue.call(atom) + + atom1 = Atom.loadOrCreate("editor") + expect(atom1.state.stuff).toBeUndefined() + + loadSettings.initialPaths = [dir1, dir2] + atom2 = Atom.loadOrCreate("editor") + expect(atom2.state.stuff).toBe("cool") diff --git a/src/atom.coffee b/src/atom.coffee index 385180dfd..8ad4780c9 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -73,7 +73,7 @@ class Atom extends Model # Loads and returns the serialized state corresponding to this window # if it exists; otherwise returns undefined. @loadState: (mode) -> - statePath = @getStatePath(mode) + statePath = @getStatePath(@getLoadSettings().initialPaths, mode) if fs.existsSync(statePath) try @@ -90,14 +90,13 @@ class Atom extends Model # Returns the path where the state for the current window will be # located if it exists. - @getStatePath: (mode) -> + @getStatePath: (paths, mode) -> switch mode when 'spec' filename = 'spec' when 'editor' - {initialPaths} = @getLoadSettings() - if initialPaths?.length > 0 - sha1 = crypto.createHash('sha1').update(initialPaths.join("\n")).digest('hex') + if paths?.length > 0 + sha1 = crypto.createHash('sha1').update(paths.join("\n")).digest('hex') filename = "editor-#{sha1}" if filename @@ -773,7 +772,7 @@ class Atom extends Model saveSync: -> stateString = JSON.stringify(@state) - if statePath = @constructor.getStatePath(@mode) + if statePath = @constructor.getStatePath(@project?.getPaths(), @mode) fs.writeFileSync(statePath, stateString, 'utf8') else @getCurrentWindow().loadSettings.windowState = stateString