path.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { PathExt } from '@jupyterlab/coreutils';
  4. const TESTPATH = 'foo/test/simple/test-path.js';
  5. describe('@jupyterlab/coreutils', () => {
  6. describe('PathExt', () => {
  7. describe('.join()', () => {
  8. it('should join the arguments and normalize the path', () => {
  9. const path = PathExt.join('foo', '../../../bar');
  10. expect(path).toBe('../../bar');
  11. });
  12. it('should not return "." for an empty path', () => {
  13. const path = PathExt.join('', '');
  14. expect(path).toBe('');
  15. });
  16. });
  17. describe('.basename()', () => {
  18. it('should return the last portion of a path', () => {
  19. expect(PathExt.basename(TESTPATH)).toBe('test-path.js');
  20. });
  21. });
  22. describe('.dirname()', () => {
  23. it('should get the directory name of a path', () => {
  24. expect(PathExt.dirname(TESTPATH)).toBe('foo/test/simple');
  25. });
  26. it('should not return "." for an empty path', () => {
  27. const path = PathExt.dirname('');
  28. expect(path).toBe('');
  29. });
  30. it('should not return "." for a path in the root directory', () => {
  31. const path = PathExt.dirname('foo.txt');
  32. expect(path).toBe('');
  33. });
  34. });
  35. describe('.extname()', () => {
  36. it('should get the file extension of the path', () => {
  37. expect(PathExt.extname(TESTPATH)).toBe('.js');
  38. });
  39. it('should only take the last occurrence of a dot', () => {
  40. expect(PathExt.extname('foo.tar.gz')).toBe('.gz');
  41. });
  42. });
  43. describe('.normalize()', () => {
  44. it('should normalize a string path', () => {
  45. const path = PathExt.normalize('./fixtures///b/../b/c.js');
  46. expect(path).toBe('fixtures/b/c.js');
  47. });
  48. it('should not return "." for an empty path', () => {
  49. const path = PathExt.normalize('');
  50. expect(path).toBe('');
  51. });
  52. });
  53. describe('.resolve()', () => {
  54. it('should resolve a sequence of paths to an absolute path on the server', () => {
  55. const path = PathExt.resolve('var/src', '../', 'file/');
  56. expect(path.indexOf('var/file')).not.toBe(-1);
  57. });
  58. });
  59. describe('.relative()', () => {
  60. it('should solve the relative path', () => {
  61. const path = PathExt.relative('var/src', 'var/apache');
  62. expect(path).toBe('../apache');
  63. });
  64. });
  65. describe('.normalizeExtension()', () => {
  66. it('should normalize a file extension to be of type `.foo`', () => {
  67. expect(PathExt.normalizeExtension('foo')).toBe('.foo');
  68. expect(PathExt.normalizeExtension('.bar')).toBe('.bar');
  69. });
  70. });
  71. });
  72. });