path.spec.ts 2.7 KB

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