UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

104 lines (102 loc) 6.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const os = require("os"); const utils_1 = require("./../../utils"); const testHelpers_1 = require("./../testHelpers"); describe("FileUtils", () => { describe("ensureDirectoryExistsSync", () => { it("should ensure the specified directory exists and the parent directories", () => { const host = testHelpers_1.getFileSystemHostWithFiles([], ["/some"]); utils_1.FileUtils.ensureDirectoryExistsSync(host, "/some/dir/path"); chai_1.expect(host.getCreatedDirectories()).to.deep.equal(["/some/dir", "/some/dir/path"]); }); }); describe("ensureDirectoryExists", () => { it("should ensure the specified directory exists and the parent directories", () => __awaiter(this, void 0, void 0, function* () { const host = testHelpers_1.getFileSystemHostWithFiles([], ["/some"]); yield utils_1.FileUtils.ensureDirectoryExists(host, "/some/dir/path"); chai_1.expect(host.getCreatedDirectories()).to.deep.equal(["/some/dir", "/some/dir/path"]); })); }); describe("getAbsoluteOrRelativePathFromPath", () => { const isWindows = os.platform() === "win32"; // too lazy to abstract this out because I'm pretty sure it works... my machine will get the windows tests and // the CI linux machine will get the other tests if (isWindows) { // uses forward slashes in result because that's what the ts compiler does it("should get the absolute path when absolute on windows", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("C:\\absolute\\path", "C:\\basedir")).to.equal("C:/absolute/path"); }); it("should get the relative path when relative on windows", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("relative\\path", "C:\\basedir")).to.equal("C:/basedir/relative/path"); }); it("should get the relative path without dots on windows", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("..\\relative\\path", "C:\\basedir")).to.equal("C:/relative/path"); }); } else { it("should get the absolute path when absolute on linux", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("/absolute/path", "/basedir")).to.equal("/absolute/path"); }); it("should get the relative path when relative on linux", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("relative/path", "/basedir")).to.equal("/basedir/relative/path"); }); it("should get the relative path without dots on linux", () => { chai_1.expect(utils_1.FileUtils.getAbsoluteOrRelativePathFromPath("../relative/path", "/basedir")).to.equal("/relative/path"); }); } }); describe("standardizeSlashes", () => { it("should change all back slashes to forward slashes", () => { chai_1.expect(utils_1.FileUtils.standardizeSlashes("/some/path\\including\\back/spaces")).to.equal("/some/path/including/back/spaces"); }); }); describe("filePathMatches", () => { it("should return false for a null path", () => { chai_1.expect(utils_1.FileUtils.filePathMatches(null, "test.ts")).to.be.false; }); it("should return false for an empty path", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("", "test.ts")).to.be.false; }); it("should return true when both are null", () => { chai_1.expect(utils_1.FileUtils.filePathMatches(null, null)).to.be.true; }); it("should return true when both are empty", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("", "")).to.be.true; }); it("should return false for empty search", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/tests.ts", "")).to.be.false; }); it("should return false for null search", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/tests.ts", null)).to.be.false; }); it("should return true for a file name only", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "test.ts")).to.be.true; }); it("should return true for a file name and dir", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "dir/test.ts")).to.be.true; }); it("should return true for a file name and dir with a slash at the front", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "/dir/test.ts")).to.be.true; }); it("should return true for a full match", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "V:/dir/test.ts")).to.be.true; }); it("should not error when the file path being searched for is longer", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "V:/dir/dir/test.ts")).to.be.false; }); it("should return false when the directory name doesn't exactly match", () => { chai_1.expect(utils_1.FileUtils.filePathMatches("V:/dir/test.ts", "ir/test.ts")).to.be.false; }); }); }); //# sourceMappingURL=fileUtilsTests.js.map