Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Own tildify function that leaves path alone if not in home
Browse files Browse the repository at this point in the history
  • Loading branch information
damieng committed Oct 11, 2016
1 parent 3ced7ae commit 5e642f1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -33,7 +33,6 @@
"async": "~0.2.9",
"mkdirp": "~0.3.5",
"rimraf": "~2.2.2",
"tildify": "^1.2.0",
"underscore-plus": "1.x"
}
}
77 changes: 66 additions & 11 deletions spec/fs-plus-spec.coffee
Expand Up @@ -503,19 +503,74 @@ describe "fs", ->
expect(fs.normalize('~/foo')).toBe path.join(fs.getHomeDirectory(), 'foo')

describe ".tildify(pathToTildify)", ->
it "tildifys the path", ->
originalPlatform = null
originalHome = null
originalSep = null
originalNormalize = null

beforeEach ->
originalPlatform = process.platform
originalHome = process.env.HOME
originalSep = path.sep
originalNormalize = path.normalize
# path.js changes per platform, mocking the above doesn't help with this function
Object.defineProperty path, 'normalize', value: (path) -> originalNormalize(path).replace(/\\/g, '/')

afterEach ->
Object.defineProperty process, 'platform', value: originalPlatform
Object.defineProperty process.env, 'HOME', value: originalHome
Object.defineProperty path, 'sep', value: originalSep
Object.defineProperty path, 'normalize', value: originalNormalize

it "tildifys the path on macOS", ->
Object.defineProperty process, 'platform', value: 'darwin'
Object.defineProperty process.env, 'HOME', value: '/Users/Buzz'
Object.defineProperty path, 'sep', value: '/'

home = fs.getHomeDirectory()
if process.platform is 'win32'
expect(fs.tildify(home)).toBe home
else
expect(fs.tildify(home)).toBe '~'
expect(fs.tildify(path.join(home, 'foo'))).toBe '~/foo'
fixture = path.join('foo', fs.getHomeDirectory())
expect(fs.tildify(fixture)).toBe fixture
fixture = path.resolve("#{home}foo", 'tildify')
expect(fs.tildify(fixture)).toBe fixture
expect(fs.tildify('foo')).toBe 'foo'
debugger
expect(fs.tildify(home)).toBe '~'
expect(fs.tildify(path.join(home, 'foo'))).toBe '~/foo'
fixture = path.join('foo', home)
expect(fs.tildify(fixture)).toBe fixture
fixture = path.resolve("#{home}foo", 'tildify')
expect(fs.tildify(fixture)).toBe fixture
expect(fs.tildify('foo')).toBe 'foo'

it "tildifys the path on Linux", ->
Object.defineProperty process, 'platform', value: 'linux'
Object.defineProperty process.env, 'HOME', value: '/home/buzz'
Object.defineProperty path, 'sep', value: '/'

home = fs.getHomeDirectory()
expect(fs.tildify(home)).toBe '~'
expect(fs.tildify(path.join(home, 'foo'))).toBe '~/foo'
fixture = path.join('foo', home)
expect(fs.tildify(fixture)).toBe fixture
fixture = path.resolve("#{home}foo", 'tildify')
expect(fs.tildify(fixture)).toBe fixture
expect(fs.tildify('foo')).toBe 'foo'

it "doesn't tildify the path on Windows", ->
Object.defineProperty process, 'platform', value: 'win32'
Object.defineProperty process.env, 'USERPROFILE', value: 'C:\\Users\\Buzz'
Object.defineProperty path, 'sep', value: '\\'

home = fs.getHomeDirectory()

expect(fs.tildify(home)).toBe home
expect(fs.tildify(path.join(home, 'foo'))).toBe path.join(home, 'foo')

it "doesn't change URLs or paths not tildified", ->
Object.defineProperty process, 'platform', value: 'darwin'
Object.defineProperty process.env, 'HOME', value: '/Users/Buzz'
Object.defineProperty path, 'sep', value: '/'

urlToLeaveAlone = "https://atom.io/something/fun?abc"
expect(fs.tildify(urlToLeaveAlone)).toBe urlToLeaveAlone

pathToLeaveAlone = "/Library/Support/Atom/State"
expect(fs.tildify(pathToLeaveAlone)).toBe pathToLeaveAlone

describe ".move", ->
tempDir = null
Expand Down
12 changes: 9 additions & 3 deletions src/fs-plus.coffee
Expand Up @@ -4,7 +4,6 @@ path = require 'path'

_ = require 'underscore-plus'
async = require 'async'
tildify = require 'tildify'
mkdirp = require 'mkdirp'
rimraf = require 'rimraf'

Expand Down Expand Up @@ -61,15 +60,22 @@ fsPlus =
return "#{fsPlus.getHomeDirectory()}#{relativePath.substring(1)}"
return relativePath

# Public: Convert an absolute path to tilde path for linux and mac:
# Public: Convert an absolute path to tilde path for Linux and macOS.
# /Users/username/dev => ~/dev
#
# pathToTildify - The {String} containing the full path.
#
# Returns a tildified path {String}.
tildify: (pathToTildify) ->
return pathToTildify if process.platform is 'win32'
return tildify(pathToTildify)

normalized = fsPlus.normalize(pathToTildify)
homeDir = fsPlus.getHomeDirectory()

return '~' if normalized is homeDir
return pathToTildify unless normalized.startsWith(path.join(homeDir, path.sep))

path.join('~', path.sep, normalized.substring(homeDir.length + 1))

# Public: Get path to store application specific data.
#
Expand Down

0 comments on commit 5e642f1

Please sign in to comment.