From 22237658cb74c12a22cb4d1d44d3f12280fc1ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 7 Dec 2017 23:33:43 -0500 Subject: [PATCH] Add some unit tests for `Helper.expandHome` --- src/helper.js | 2 ++ test/src/helperTest.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/src/helperTest.js diff --git a/src/helper.js b/src/helper.js index c34e16ac..052e4115 100644 --- a/src/helper.js +++ b/src/helper.js @@ -157,6 +157,8 @@ function ip2hex(address) { }).join(""); } +// Expand ~ into the current user home dir. +// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`. function expandHome(shortenedPath) { if (!shortenedPath) { return ""; diff --git a/test/src/helperTest.js b/test/src/helperTest.js new file mode 100644 index 00000000..36d1b518 --- /dev/null +++ b/test/src/helperTest.js @@ -0,0 +1,39 @@ +"use strict"; + +const expect = require("chai").expect; +const os = require("os"); +const Helper = require("../../src/helper"); + +describe("Helper", function() { + describe("#expandHome", function() { + it("should correctly expand a Unix path", function() { + expect([`${os.homedir()}/tmp`, `${os.homedir()}\\tmp`]) + .to.include(Helper.expandHome("~/tmp")); + }); + + it("should correctly expand a Windows path", function() { + expect(Helper.expandHome("~\\tmp")).to.equal(`${os.homedir()}\\tmp`); + }); + + it("should correctly expand when not given a specific path", function() { + expect(Helper.expandHome("~")).to.equal(os.homedir()); + }); + + it("should not expand paths not starting with tilde", function() { + expect(Helper.expandHome("/tmp")).to.match(/^\/tmp|[A-Z]:\\tmp$/); + }); + + it("should not expand a tilde in the middle of a string", function() { + expect(Helper.expandHome("/tmp/~foo")) + .to.match(/^\/tmp\/~foo|[A-Z]:\\tmp\\~foo$/); + }); + + it("should return an empty string when given an empty string", function() { + expect(Helper.expandHome("")).to.equal(""); + }); + + it("should return an empty string when given undefined", function() { + expect(Helper.expandHome(undefined)).to.equal(""); + }); + }); +});