/packages/common/src/tests/fileSystem/fileUtilsTests.ts

https://github.com/dsherret/ts-morph · TypeScript · 222 lines · 169 code · 52 blank · 1 comment · 0 complexity · ece14b3438236f5abddbaaf0b012f91f MD5 · raw file

  1. import { expect } from "chai";
  2. import { FileUtils, InMemoryFileSystemHost } from "../../fileSystem";
  3. describe(nameof(FileUtils), () => {
  4. describe(nameof(FileUtils.getStandardizedAbsolutePath), () => {
  5. const fileSystem = new InMemoryFileSystemHost();
  6. it("should get the absolute path when absolute", () => {
  7. expect(FileUtils.getStandardizedAbsolutePath(fileSystem, "/absolute/path", "/basedir")).to.equal("/absolute/path");
  8. });
  9. it("should get the relative path when relative", () => {
  10. expect(FileUtils.getStandardizedAbsolutePath(fileSystem, "relative/path", "/basedir")).to.equal("/basedir/relative/path");
  11. });
  12. it("should get the relative path without dots", () => {
  13. expect(FileUtils.getStandardizedAbsolutePath(fileSystem, "../relative/path", "/basedir")).to.equal("/relative/path");
  14. });
  15. });
  16. describe(nameof(FileUtils.standardizeSlashes), () => {
  17. it("should change all back slashes to forward slashes", () => {
  18. expect(FileUtils.standardizeSlashes("/some/path\\including\\back/spaces")).to.equal("/some/path/including/back/spaces");
  19. });
  20. });
  21. describe(nameof(FileUtils.pathStartsWith), () => {
  22. it("should return false for a undefined path", () => {
  23. expect(FileUtils.pathStartsWith(undefined, "test.ts")).to.be.false;
  24. });
  25. it("should return false for an empty path", () => {
  26. expect(FileUtils.pathStartsWith("", "test.ts")).to.be.false;
  27. });
  28. it("should return true when both are undefined", () => {
  29. expect(FileUtils.pathStartsWith(undefined, undefined)).to.be.true;
  30. });
  31. it("should return true when both are empty", () => {
  32. expect(FileUtils.pathStartsWith("", "")).to.be.true;
  33. });
  34. it("should return true for the root directory", () => {
  35. expect(FileUtils.pathStartsWith("/dir", "/")).to.be.true;
  36. });
  37. it("should return true for the root directory on windows", () => {
  38. expect(FileUtils.pathStartsWith("C:/dir", "C:/")).to.be.true;
  39. });
  40. it("should return false for empty search", () => {
  41. expect(FileUtils.pathStartsWith("V:/dir/tests.ts", "")).to.be.false;
  42. });
  43. it("should return false for undefined search", () => {
  44. expect(FileUtils.pathStartsWith("V:/dir/tests.ts", undefined)).to.be.false;
  45. });
  46. it("should return false for a file name only", () => {
  47. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "test.ts")).to.be.false;
  48. });
  49. it("should return true when matches start directory without a slash", () => {
  50. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "V:/dir")).to.be.true;
  51. });
  52. it("should return true when matches start directory with a slash", () => {
  53. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "V:/dir/")).to.be.true;
  54. });
  55. it("should return true for a full match", () => {
  56. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "V:/dir/test.ts")).to.be.true;
  57. });
  58. it("should not error when the file path being searched for is longer", () => {
  59. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "V:/dir/dir/test.ts")).to.be.false;
  60. });
  61. it("should return false when the end name doesn't exactly match", () => {
  62. expect(FileUtils.pathStartsWith("V:/dir/test.ts", "V:/dir/test.t")).to.be.false;
  63. });
  64. });
  65. describe(nameof(FileUtils.pathEndsWith), () => {
  66. it("should return false for a undefined path", () => {
  67. expect(FileUtils.pathEndsWith(undefined, "test.ts")).to.be.false;
  68. });
  69. it("should return false for an empty path", () => {
  70. expect(FileUtils.pathEndsWith("", "test.ts")).to.be.false;
  71. });
  72. it("should return true when both are undefined", () => {
  73. expect(FileUtils.pathEndsWith(undefined, undefined)).to.be.true;
  74. });
  75. it("should return true when both are empty", () => {
  76. expect(FileUtils.pathEndsWith("", "")).to.be.true;
  77. });
  78. it("should return false for empty search", () => {
  79. expect(FileUtils.pathEndsWith("V:/dir/tests.ts", "")).to.be.false;
  80. });
  81. it("should return false for undefined search", () => {
  82. expect(FileUtils.pathEndsWith("V:/dir/tests.ts", undefined)).to.be.false;
  83. });
  84. it("should return true for a file name only", () => {
  85. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "test.ts")).to.be.true;
  86. });
  87. it("should return true for a file name and dir", () => {
  88. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "dir/test.ts")).to.be.true;
  89. });
  90. it("should return true for a file name and dir with a slash at the front", () => {
  91. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "/dir/test.ts")).to.be.true;
  92. });
  93. it("should return true for a full match", () => {
  94. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "V:/dir/test.ts")).to.be.true;
  95. });
  96. it("should not error when the file path being searched for is longer", () => {
  97. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "V:/dir/dir/test.ts")).to.be.false;
  98. });
  99. it("should return false when the directory name doesn't exactly match", () => {
  100. expect(FileUtils.pathEndsWith("V:/dir/test.ts", "ir/test.ts")).to.be.false;
  101. });
  102. });
  103. describe(nameof(FileUtils.getParentMostPaths), () => {
  104. function doTest(paths: string[], expected: string[]) {
  105. expect(FileUtils.getParentMostPaths(paths).sort()).to.deep.equal(expected.sort());
  106. }
  107. it("should get the parent-most paths", () => {
  108. doTest(["/dir/child", "/dir", "/dir/child/2"], ["/dir"]);
  109. });
  110. it("should get the parent-most paths for sub directories", () => {
  111. doTest(["/dir/child", "/dir/child2"], ["/dir/child", "/dir/child2"]);
  112. });
  113. });
  114. describe(nameof(FileUtils.getRelativePathTo), () => {
  115. function doTest(from: string, to: string, expected: string) {
  116. expect(FileUtils.getRelativePathTo(from, to)).to.equal(expected);
  117. }
  118. it("should get the relative path when the file is in the parent directory", () => {
  119. doTest("V:/testing/this/out", "V:/testing/this/to.ts", "../to.ts");
  120. });
  121. it("should get the relative path when the file is in a child directory", () => {
  122. doTest("V:/testing/this", "V:/testing/this/out/to.ts", "out/to.ts");
  123. });
  124. it("should get the relative path when the files are in different child directories", () => {
  125. doTest("V:/testing/this/child1", "V:/testing/this/child2/to.ts", "../child2/to.ts");
  126. });
  127. it("should get the relative path when the files are in the same directory", () => {
  128. doTest("V:/testing/this/out", "V:/testing/this/out/to.ts", "to.ts");
  129. });
  130. it("should get the relative path when the files are the same", () => {
  131. doTest("V:/testing/this/out", "V:/testing/this/out/to.ts", "to.ts");
  132. });
  133. });
  134. describe(nameof(FileUtils.getExtension), () => {
  135. function doTest(path: string, expected: string) {
  136. expect(FileUtils.getExtension(path)).to.equal(expected);
  137. }
  138. // copying behaviour from https://nodejs.org/api/path.html#path_path_extname_path
  139. it("should return a dot for a file that ends with a dot", () => {
  140. doTest("path/file.", ".");
  141. });
  142. it("should return only the last extension for a file name with multiple dots", () => {
  143. doTest("path/file.coffee.md", ".md");
  144. });
  145. it("should return an empty string when there's no dot in the file name", () => {
  146. doTest("path/file", "");
  147. });
  148. it("should return an empty string when the dot is at the start of the file name", () => {
  149. doTest("path/.file", "");
  150. });
  151. it("should return an empty string when there's no dot in the file name and a dot in the directory path", () => {
  152. doTest("path.something/file", "");
  153. });
  154. it("should return the extension for a .ts file", () => {
  155. doTest("path/file.ts", ".ts");
  156. });
  157. it("should return the extension for a .d.ts file", () => {
  158. doTest("path/file.d.ts", ".d.ts");
  159. });
  160. it("should return the extension for a .d.ts file that's upper case", () => {
  161. doTest("path/file.D.TS", ".D.TS");
  162. });
  163. it("should return the extension for a .js.map file", () => {
  164. doTest("path/file.js.map", ".js.map");
  165. });
  166. it("should return the extension for a .js.map file that's upper case", () => {
  167. doTest("path/file.JS.MAP", ".JS.MAP");
  168. });
  169. });
  170. });