diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee index a4d5b2bc6..9f2524fa0 100644 --- a/spec/atom/buffer-spec.coffee +++ b/spec/atom/buffer-spec.coffee @@ -3,11 +3,47 @@ fs = require 'fs' describe 'Buffer', -> describe 'constructor', -> - it "loads the contents of the given url", -> - filePath = require.resolve 'fixtures/sample.txt' - buffer = new Buffer filePath - expect(buffer.getText()).toBe fs.read(filePath) + describe "when given a url", -> + describe "when a file exists for the url", -> + it "loads the contents of that file", -> + filePath = require.resolve 'fixtures/sample.txt' + buffer = new Buffer filePath + expect(buffer.getText()).toBe fs.read(filePath) + + describe "when no file exists for the url", -> + it "creates an empty buffer", -> + filePath = "does-not-exist.txt" + expect(fs.exists(filePath)).toBeFalsy() + + buffer = new Buffer filePath + expect(buffer.getText()).toBe "" + + describe "when no url is given", -> + it "creates an empty buffer", -> + buffer = new Buffer null + expect(buffer.getText()).toBe "" + + describe "save", -> + describe "when the buffer has a url", -> + filePath = null + + beforeEach -> + filePath = require.resolve('fixtures') + '/temp.txt' + expect(fs.exists(filePath)).toBeFalsy() + + afterEach -> + fs.remove filePath + + it "saves the contents of the buffer to the url", -> + buffer = new Buffer filePath + buffer.setText 'Buffer contents!' + buffer.save() + expect(fs.read(filePath)).toEqual 'Buffer contents!' + + describe "when the buffer no url", -> + it "throw an exception", -> + buffer = new Buffer + expect(-> buffer.save()).toThrow() + + - it "loads an empty buffer if no url is given", -> - buffer = new Buffer null - expect(buffer.getText()).toBe "" diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index 86b4b2a09..54edc10f6 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -7,9 +7,18 @@ class Buffer url: null constructor: (@url) -> - text = if @url then fs.read(@url) else "" + text = if @url and fs.exists(@url) + fs.read(@url) + else + "" @aceDocument = new Document text getText: -> @aceDocument.getValue() + setText: (text) -> + @aceDocument.setValue text + + save: -> + if not @url then throw new Error("Tried to save buffer with no url") + fs.write @url, @getText() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index e077543fc..ffb32fd93 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -25,4 +25,3 @@ class Editor getAceSession: -> @aceEditor.getSession() - diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 34f881808..57fa14789 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -67,6 +67,12 @@ module.exports = listDirectoryTree: (path) -> @list path, true + # Remove a file at the given path. Throws an error if path is not a + # file or a symbolic link to a file. + remove: (path) -> + fm = OSX.NSFileManager.defaultManager + paths = fm.removeItemAtPath_error path, null + # Open, read, and close a file, returning the file's contents. read: (path) -> path = @absolute path