Use path.dirname() instead of fsUtils.directory()

This commit is contained in:
Kevin Sawicki
2013-06-12 15:54:34 -07:00
parent d1cf839e53
commit 7cf4063d9e
10 changed files with 27 additions and 46 deletions

View File

@@ -1,19 +1,20 @@
File = require 'file'
fsUtils = require 'fs-utils'
path = require 'path'
describe 'File', ->
[path, file] = []
[filePath, file] = []
beforeEach ->
path = fsUtils.join(fsUtils.resolveOnLoadPath('fixtures'), "atom-file-test.txt") # Don't put in /tmp because /tmp symlinks to /private/tmp and screws up the rename test
fsUtils.remove(path) if fsUtils.exists(path)
fsUtils.write(path, "this is old!")
file = new File(path)
filePath = fsUtils.join(fsUtils.resolveOnLoadPath('fixtures'), "atom-file-test.txt") # Don't put in /tmp because /tmp symlinks to /private/tmp and screws up the rename test
fsUtils.remove(filePath) if fsUtils.exists(filePath)
fsUtils.write(filePath, "this is old!")
file = new File(filePath)
file.read()
afterEach ->
file.off()
fsUtils.remove(path) if fsUtils.exists(path)
fsUtils.remove(filePath) if fsUtils.exists(filePath)
describe "when the contents of the file change", ->
it "triggers 'contents-changed' event handlers", ->
@@ -46,7 +47,7 @@ describe 'File', ->
newPath = null
beforeEach ->
newPath = fsUtils.join(fsUtils.directory(path), "atom-file-was-moved-test.txt")
newPath = fsUtils.join(path.dirname(filePath), "atom-file-was-moved-test.txt")
afterEach ->
if fsUtils.exists(newPath)
@@ -59,7 +60,7 @@ describe 'File', ->
moveHandler = jasmine.createSpy('moveHandler')
file.on 'moved', moveHandler
fsUtils.move(path, newPath)
fsUtils.move(filePath, newPath)
waitsFor "move event", ->
moveHandler.callCount > 0
@@ -76,7 +77,7 @@ describe 'File', ->
changeHandler = jasmine.createSpy('changeHandler')
file.on 'contents-changed', changeHandler
fsUtils.move(path, newPath)
fsUtils.move(filePath, newPath)
waitsFor "move event", ->
moveHandler.callCount > 0
@@ -100,12 +101,12 @@ describe 'File', ->
expect(changeHandler).not.toHaveBeenCalled()
fsUtils.remove(path)
fsUtils.remove(filePath)
expect(changeHandler).not.toHaveBeenCalled()
waits 20
runs ->
fsUtils.write(path, "HE HAS RISEN!")
fsUtils.write(filePath, "HE HAS RISEN!")
expect(changeHandler).not.toHaveBeenCalled()
waitsFor "resurrection change event", ->
@@ -113,7 +114,7 @@ describe 'File', ->
runs ->
expect(removeHandler).not.toHaveBeenCalled()
fsUtils.write(path, "Hallelujah!")
fsUtils.write(filePath, "Hallelujah!")
changeHandler.reset()
waitsFor "post-resurrection change event", ->

View File

@@ -21,18 +21,6 @@ describe "fsUtils", ->
expect(fsUtils.isFile(fsUtils.join(fixturesDir, 'non-existent'))).toBe false
expect(fsUtils.isFile(null)).toBe false
describe ".directory(path)", ->
describe "when called with a file path", ->
it "returns the path to the directory", ->
expect(fsUtils.directory(fsUtils.resolveOnLoadPath('fixtures/dir/a'))).toBe fsUtils.resolveOnLoadPath('fixtures/dir')
describe "when called with a directory path", ->
it "return the path it was given", ->
expect(fsUtils.directory("/a/b/c")).toBe "/a/b"
expect(fsUtils.directory("/a")).toBe ""
expect(fsUtils.directory("a")).toBe ""
expect(fsUtils.directory("/a/b/c++")).toBe "/a/b"
describe ".exists(path)", ->
it "returns true when path exsits", ->
expect(fsUtils.exists(fsUtils.resolveOnLoadPath('fixtures'))).toBe true

View File

@@ -50,7 +50,7 @@ class EditSession
@buffer.retain()
@subscribe @buffer, "path-changed", =>
@project.setPath(fsUtils.directory(@getPath())) unless @project.getPath()?
@project.setPath(path.dirname(@getPath())) unless @project.getPath()?
@trigger "title-changed"
@trigger "path-changed"
@subscribe @buffer, "contents-conflicted", => @trigger "contents-conflicted"
@@ -112,7 +112,7 @@ class EditSession
getLongTitle: ->
if sessionPath = @getPath()
fileName = path.basename(sessionPath)
directory = path.basename(fsUtils.directory(sessionPath))
directory = path.basename(path.dirname(sessionPath))
"#{fileName} - #{directory}"
else
'untitled'

View File

@@ -52,12 +52,12 @@ class Project
# Sets the project path.
#
# path - A {String} representing the new path
setPath: (path) ->
# projectPath - A {String} representing the new path
setPath: (projectPath) ->
@rootDirectory?.off()
if path?
directory = if fsUtils.isDirectory(path) then path else fsUtils.directory(path)
if projectPath?
directory = if fsUtils.isDirectory(projectPath) then projectPath else path.dirname(projectPath)
@rootDirectory = new Directory(directory)
else
@rootDirectory = null

View File

@@ -46,7 +46,7 @@ class PackageGeneratorView extends View
getPackagePath: ->
packagePath = @miniEditor.getText()
packageName = _.dasherize(path.basename(packagePath))
fsUtils.join(fsUtils.directory(packagePath), packageName)
fsUtils.join(path.dirname(packagePath), packageName)
validPackagePath: ->
if fsUtils.exists(@getPackagePath())
@@ -70,7 +70,7 @@ class PackageGeneratorView extends View
if fsUtils.isDirectory(path)
fsUtils.makeTree(sourcePath)
if fsUtils.isFile(path)
fsUtils.makeTree(fsUtils.directory(sourcePath))
fsUtils.makeTree(path.dirname(sourcePath))
content = @replacePackageNamePlaceholders(fsUtils.read(path), packageName)
fsUtils.write(sourcePath, content)

View File

@@ -42,14 +42,14 @@ describe 'Package Generator', ->
it "forces the package's name to be lowercase with dashes", ->
packageName = "CamelCaseIsForTheBirds"
packagePath = fsUtils.join(fsUtils.directory(packagePath), packageName)
packagePath = fsUtils.join(path.dirname(packagePath), packageName)
rootView.trigger("package-generator:generate")
packageGeneratorView = rootView.find(".package-generator").view()
packageGeneratorView.miniEditor.setText(packagePath)
packageGeneratorView.trigger "core:confirm"
expect(packagePath).not.toExistOnDisk()
expect(fsUtils.join(fsUtils.directory(packagePath), "camel-case-is-for-the-birds")).toExistOnDisk()
expect(fsUtils.join(path.dirname(packagePath), "camel-case-is-for-the-birds")).toExistOnDisk()
it "correctly lays out the package files and closes the package generator view", ->
rootView.attachToDom()

View File

@@ -47,7 +47,7 @@ class TabView extends View
if fileNameText?
duplicates = @editor.getEditSessions().filter (session) -> fileNameText is session.buffer.getBaseName()
if duplicates.length > 1
directory = path.basename(fsUtils.directory(@editSession.getPath()))
directory = path.basename(path.dirname(@editSession.getPath()))
fileNameText = "#{fileNameText} - #{directory}" if directory
else
fileNameText = 'untitled'

View File

@@ -235,7 +235,7 @@ class TreeView extends ScrollView
dialog.showError("Error: #{newPath} already exists. Try a different path.")
return
directoryPath = fsUtils.directory(newPath)
directoryPath = path.dirname(newPath)
try
fsUtils.makeTree(directoryPath) unless fsUtils.exists(directoryPath)
fsUtils.move(oldPath, newPath)
@@ -261,7 +261,7 @@ class TreeView extends ScrollView
add: ->
selectedEntry = @selectedEntry() or @root
selectedPath = selectedEntry.getPath()
directoryPath = if fsUtils.isFile(selectedPath) then fsUtils.directory(selectedPath) else selectedPath
directoryPath = if fsUtils.isFile(selectedPath) then path.dirname(selectedPath) else selectedPath
relativeDirectoryPath = project.relativize(directoryPath)
relativeDirectoryPath += '/' if relativeDirectoryPath.length > 0

View File

@@ -6,7 +6,7 @@ mkdirp = require 'mkdirp'
fsUtils = require 'fs-utils'
symlinkCommand = (sourcePath, destinationPath, callback) ->
mkdirp fsUtils.directory(destinationPath), (error) ->
mkdirp path.dirname(destinationPath), (error) ->
if error?
callback(error)
else

View File

@@ -23,14 +23,6 @@ module.exports =
catch e
path
# Returns the path of a file's containing directory, albeit the
# parent directory if the file is a directory. A terminal directory
# separator is ignored.
directory: (path) ->
parentPath = path.replace(new RegExp("/#{Path.basename(_.escapeRegExp(path))}\/?$"), '')
return "" if path == parentPath
parentPath
# Returns true if the file specified by path exists
exists: (path) ->
path? and fs.existsSync(path)