 {readFileSync} = fs
 {join} = require 'path'
 temp = require 'temp'
+{File} = require 'pathwatcher'
 TextBuffer = require '../src/text-buffer'
 SampleText = readFileSync(join(__dirname, 'fixtures', 'sample.js'), 'utf8')
 
 describe "TextBuffer", ->
   buffer = null
+
+  afterEach ->
+    buffer = null
 
   describe "construction", ->
     it "can be constructed empty", ->


      runs ->
        expect(buffer.isModified()).toBeFalsy()

      bufferB.undo()
      expect(bufferB.getText()).toBe "hello\nworld\r\nhow are you doing?"

    describe "when the buffer has a path", ->
      [filePath, buffer2] = []

      beforeEach ->
        filePath = temp.openSync('atom').path
        fs.writeFileSync(filePath, "words")
        buffer = new TextBuffer({filePath, load: true})

        waitsFor ->
          buffer.loaded

      afterEach ->
        buffer2?.destroy()

      describe "when the serialized buffer had no unsaved changes", ->
        it "loads the current contents of the file at the serialized path", ->
          expect(buffer.isModified()).toBeFalsy()
          buffer2 = buffer.testSerialization()
          buffer2.load()

          waitsFor ->
            buffer2.loaded

          runs ->
            expect(buffer2.isModified()).toBeFalsy()
            expect(buffer2.getPath()).toBe(buffer.getPath())
            expect(buffer2.getText()).toBe(buffer.getText())

      describe "when the serialized buffer had unsaved changes", ->
        describe "when the disk contents were changed since serialization", ->
          it "loads the disk contents instead of the previous unsaved state", ->
            buffer.setText("BUFFER CHANGE")
            fs.writeFileSync(filePath, "DISK CHANGE")

            buffer2 = buffer.testSerialization()

            waitsFor ->
              buffer2.cachedDiskContents

            runs ->
              expect(buffer2.getPath()).toBe(buffer.getPath())
              expect(buffer2.getText()).toBe("DISK CHANGE")
              expect(buffer2.isModified()).toBeFalsy()

        describe "when the disk contents are the same since serialization", ->
          it "restores the previous unsaved state of the buffer", ->
            previousText = buffer.getText()
            buffer.setText("abc")

            buffer2 = buffer.testSerialization()
            buffer2.load()

            waitsFor ->
              buffer2.loaded

            runs ->
              expect(buffer2.getPath()).toBe(buffer.getPath())
              expect(buffer2.getText()).toBe(buffer.getText())
              expect(buffer2.isModified()).toBeTruthy()
              buffer2.setText(previousText)
              expect(buffer2.isModified()).toBeFalsy()

      describe "when the serialized buffer was unsaved and had no path", ->
        it "restores the previous unsaved state of the buffer", ->
          buffer.destroy()

          buffer = new TextBuffer()
          buffer.setText("abc")

          buffer2 = buffer.testSerialization()
          expect(buffer2.getPath()).toBeUndefined()
          expect(buffer2.getText()).toBe("abc")

  describe "::getRange()", ->
    it "returns the range of the entire buffer text", ->
      buffer = new TextBuffer("abc\ndef\nghi")
