|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +import { StringBufferTerminalProvider, Terminal } from '@rushstack/terminal'; |
| 7 | + |
| 8 | +import { RushConfiguration } from '../../../api/RushConfiguration'; |
| 9 | +import { PathProjectSelectorParser } from '../PathProjectSelectorParser'; |
| 10 | + |
| 11 | +describe(PathProjectSelectorParser.name, () => { |
| 12 | + let rushConfiguration: RushConfiguration; |
| 13 | + let terminal: Terminal; |
| 14 | + let terminalProvider: StringBufferTerminalProvider; |
| 15 | + let parser: PathProjectSelectorParser; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + const rushJsonFile: string = path.resolve(__dirname, '../../../api/test/repo/rush-npm.json'); |
| 19 | + rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); |
| 20 | + terminalProvider = new StringBufferTerminalProvider(); |
| 21 | + terminal = new Terminal(terminalProvider); |
| 22 | + parser = new PathProjectSelectorParser(rushConfiguration, rushConfiguration.rushJsonFolder); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should select a project by exact path', async () => { |
| 26 | + const result = await parser.evaluateSelectorAsync({ |
| 27 | + unscopedSelector: 'project1', |
| 28 | + terminal, |
| 29 | + parameterName: '--only' |
| 30 | + }); |
| 31 | + |
| 32 | + const projects = Array.from(result); |
| 33 | + expect(projects).toHaveLength(1); |
| 34 | + expect(projects[0].packageName).toBe('project1'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should select a project by path within the project', async () => { |
| 38 | + const result = await parser.evaluateSelectorAsync({ |
| 39 | + unscopedSelector: 'project1/src/index.ts', |
| 40 | + terminal, |
| 41 | + parameterName: '--only' |
| 42 | + }); |
| 43 | + |
| 44 | + const projects = Array.from(result); |
| 45 | + expect(projects).toHaveLength(1); |
| 46 | + expect(projects[0].packageName).toBe('project1'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should select multiple projects from a parent directory', async () => { |
| 50 | + const result = await parser.evaluateSelectorAsync({ |
| 51 | + unscopedSelector: '.', |
| 52 | + terminal, |
| 53 | + parameterName: '--only' |
| 54 | + }); |
| 55 | + |
| 56 | + const projects = Array.from(result); |
| 57 | + expect(projects.length).toBeGreaterThan(0); |
| 58 | + // Should include all projects in the test repo |
| 59 | + const packageNames = projects.map((p) => p.packageName).sort(); |
| 60 | + expect(packageNames).toContain('project1'); |
| 61 | + expect(packageNames).toContain('project2'); |
| 62 | + expect(packageNames).toContain('project3'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should select project from specified directory', async () => { |
| 66 | + const project1Path = path.join(rushConfiguration.rushJsonFolder, 'project1'); |
| 67 | + const parserWithCustomCwd = new PathProjectSelectorParser(rushConfiguration, project1Path); |
| 68 | + |
| 69 | + const result = await parserWithCustomCwd.evaluateSelectorAsync({ |
| 70 | + unscopedSelector: '.', |
| 71 | + terminal, |
| 72 | + parameterName: '--only' |
| 73 | + }); |
| 74 | + |
| 75 | + const projects = Array.from(result); |
| 76 | + expect(projects).toHaveLength(1); |
| 77 | + expect(projects[0].packageName).toBe('project1'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle absolute paths', async () => { |
| 81 | + const absolutePath = path.join(rushConfiguration.rushJsonFolder, 'project2'); |
| 82 | + |
| 83 | + const result = await parser.evaluateSelectorAsync({ |
| 84 | + unscopedSelector: absolutePath, |
| 85 | + terminal, |
| 86 | + parameterName: '--only' |
| 87 | + }); |
| 88 | + |
| 89 | + const projects = Array.from(result); |
| 90 | + expect(projects).toHaveLength(1); |
| 91 | + expect(projects[0].packageName).toBe('project2'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw error for paths that do not match any project', async () => { |
| 95 | + await expect( |
| 96 | + parser.evaluateSelectorAsync({ |
| 97 | + unscopedSelector: 'nonexistent/path', |
| 98 | + terminal, |
| 99 | + parameterName: '--only' |
| 100 | + }) |
| 101 | + ).rejects.toThrow(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should handle paths outside workspace', async () => { |
| 105 | + // Paths outside the workspace should not match any project and throw |
| 106 | + await expect( |
| 107 | + parser.evaluateSelectorAsync({ |
| 108 | + unscopedSelector: '../outside', |
| 109 | + terminal, |
| 110 | + parameterName: '--only' |
| 111 | + }) |
| 112 | + ).rejects.toThrow(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return empty completions', () => { |
| 116 | + const completions = Array.from(parser.getCompletions()); |
| 117 | + expect(completions).toHaveLength(0); |
| 118 | + }); |
| 119 | +}); |
0 commit comments