|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { generateNameFromPath } from './test-discovery'; |
| 10 | + |
| 11 | +describe('generateNameFromPath', () => { |
| 12 | + const roots = ['/project/src/', '/project/']; |
| 13 | + |
| 14 | + it('should generate a dash-cased name from a simple path', () => { |
| 15 | + const testFile = '/project/src/app/components/my-component.spec.ts'; |
| 16 | + const result = generateNameFromPath(testFile, roots, true); |
| 17 | + expect(result).toBe('app-components-my-component'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle Windows-style paths', () => { |
| 21 | + const testFile = 'C:\\project\\src\\app\\components\\my-component.spec.ts'; |
| 22 | + const result = generateNameFromPath(testFile, ['C:\\project\\src\\'], true); |
| 23 | + expect(result).toBe('app-components-my-component'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should remove test extensions when removeTestExtension is true', () => { |
| 27 | + const testFile = '/project/src/app/utils/helpers.test.ts'; |
| 28 | + const result = generateNameFromPath(testFile, roots, true); |
| 29 | + expect(result).toBe('app-utils-helpers'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should not remove test extensions when removeTestExtension is false', () => { |
| 33 | + const testFile = '/project/src/app/utils/helpers.test.ts'; |
| 34 | + const result = generateNameFromPath(testFile, roots, false); |
| 35 | + expect(result).toBe('app-utils-helpers.test'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle paths with leading dots and slashes', () => { |
| 39 | + const testFile = '/project/src/./app/services/api.service.spec.ts'; |
| 40 | + const result = generateNameFromPath(testFile, roots, true); |
| 41 | + expect(result).toBe('app-services-api.service'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return the basename if no root matches', () => { |
| 45 | + const testFile = '/unrelated/path/to/some/file.spec.ts'; |
| 46 | + const result = generateNameFromPath(testFile, roots, true); |
| 47 | + expect(result).toBe('file'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should truncate a long file name', () => { |
| 51 | + const longPath = |
| 52 | + 'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/in/the/function.spec.ts'; // eslint-disable-line max-len |
| 53 | + const testFile = `/project/src/${longPath}`; |
| 54 | + const result = generateNameFromPath(testFile, roots, true); |
| 55 | + |
| 56 | + expect(result.length).toBeLessThanOrEqual(128); |
| 57 | + expect(result).toBe( |
| 58 | + 'a-very-long-path-that-definitely-exceeds-the-maximum-allowe-9cf40291-me-in-order-to-trigger-the-truncation-logic-in-the-function', |
| 59 | + ); // eslint-disable-line max-len |
| 60 | + }); |
| 61 | + |
| 62 | + it('should generate different hashes for different paths with similar truncated names', () => { |
| 63 | + const longPath1 = |
| 64 | + 'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/variant-a.spec.ts'; // eslint-disable-line max-len |
| 65 | + const longPath2 = |
| 66 | + 'a/very/long/path/that/definitely/exceeds/the/maximum/allowed/length/for/a/filename/in/order/to/trigger/the/truncation/logic/variant-b.spec.ts'; // eslint-disable-line max-len |
| 67 | + |
| 68 | + const testFile1 = `/project/src/${longPath1}`; |
| 69 | + const testFile2 = `/project/src/${longPath2}`; |
| 70 | + |
| 71 | + const result1 = generateNameFromPath(testFile1, roots, true); |
| 72 | + const result2 = generateNameFromPath(testFile2, roots, true); |
| 73 | + |
| 74 | + expect(result1).not.toBe(result2); |
| 75 | + // The hash is always 8 characters long and is surrounded by hyphens. |
| 76 | + const hashRegex = /-[a-f0-9]{8}-/; |
| 77 | + const hash1 = result1.match(hashRegex)?.[0]; |
| 78 | + const hash2 = result2.match(hashRegex)?.[0]; |
| 79 | + |
| 80 | + expect(hash1).toBeDefined(); |
| 81 | + expect(hash2).toBeDefined(); |
| 82 | + expect(hash1).not.toBe(hash2); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should not truncate a filename that is exactly the max length', () => { |
| 86 | + const name = 'a'.repeat(128); |
| 87 | + const testFile = `/project/src/${name}.spec.ts`; |
| 88 | + const result = generateNameFromPath(testFile, roots, true); |
| 89 | + expect(result).toBe(name); |
| 90 | + }); |
| 91 | +}); |
0 commit comments