When project's constructor is given a file as its path, it sets its path to the parent directory of the given file

This commit is contained in:
Corey Johnson
2012-05-01 09:18:12 -07:00
parent acae9d63ca
commit 28f39cbdab
4 changed files with 29 additions and 5 deletions

View File

@@ -18,7 +18,7 @@ describe "Project", ->
buffer = project.open(absolutePath)
expect(buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith buffer
describe "when given a relative path that hasn't been opened previously", ->
it "returns a buffer for the given path (relative to the project root) and emits a 'new-buffer' event", ->
buffer = project.open('a')
@@ -60,3 +60,16 @@ describe "Project", ->
expect(project.relativize(fs.join(absolutePath, "b"))).toBe "b"
expect(project.relativize(fs.join(absolutePath, "b/file.coffee"))).toBe "b/file.coffee"
expect(project.relativize(fs.join(absolutePath, "file.coffee"))).toBe "file.coffee"
describe ".setPath(path)", ->
describe "when path is a file", ->
it "sets its path to the files parent directory and updates the root directory", ->
project.setPath(require.resolve('fixtures/dir/a'))
expect(project.getPath()).toEqual require.resolve('fixtures/dir')
expect(project.getRootDirectory().path).toEqual require.resolve('fixtures/dir')
describe "when path is a directory", ->
it "sets its path to the directory and updates the root directory", ->
project.setPath(require.resolve('fixtures/dir/a-dir'))
expect(project.getPath()).toEqual require.resolve('fixtures/dir/a-dir')
expect(project.getRootDirectory().path).toEqual require.resolve('fixtures/dir/a-dir')

View File

@@ -7,6 +7,8 @@ module.exports =
class Directory
@idCounter = 0
path: null
constructor: (@path) ->
@id = ++Directory.idCounter

View File

@@ -6,10 +6,20 @@ Directory = require 'directory'
module.exports =
class Project
path: null
rootDirectory: null
buffers: null
constructor: (@path) ->
constructor: (path) ->
@setPath(path)
@buffers = []
getPath: ->
@path
setPath: (path) ->
@path = if fs.isDirectory(path) then path else fs.directory(path)
@rootDirectory.off() if @rootDirectory
@rootDirectory = new Directory(@path)
getRootDirectory: ->

View File

@@ -35,14 +35,13 @@ class RootView extends View
@setTitle(@project?.path)
@on 'active-editor-path-change', (e, path) =>
@project.path ?= fs.directory(path) if path
@project.setPath(path) unless @project.getPath()
@setTitle(path)
@commandPanel = new CommandPanel({rootView: this})
if pathToOpen?
@project = new Project(fs.directory(pathToOpen))
@project = new Project(pathToOpen)
@open(pathToOpen) if fs.isFile(pathToOpen)
else
@project = new Project(projectPath)