|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { CommandError } from '../../host'; |
| 9 | +import assert from 'node:assert'; |
| 10 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 11 | +import { join } from 'node:path'; |
| 12 | +import { AngularWorkspace } from '../../../../utilities/config'; |
| 13 | +import { CommandError, createRootRestrictedHost } from '../../host'; |
10 | 14 | import type { MockHost } from '../../testing/mock-host'; |
11 | 15 | import { |
12 | 16 | type MockMcpToolContext, |
13 | 17 | addProjectToWorkspace, |
14 | 18 | createMockContext, |
| 19 | + createMockHost, |
15 | 20 | } from '../../testing/test-utils'; |
16 | 21 | import { runTarget } from './run-target'; |
17 | 22 |
|
@@ -144,5 +149,122 @@ describe('Run Target Tool', () => { |
144 | 149 | await expectAsync( |
145 | 150 | runTarget({ target: 'build', options: { watch: true } }, mockContext), |
146 | 151 | ).toBeRejectedWithError(/Watch mode execution.*is not yet supported/); |
| 152 | + await expectAsync( |
| 153 | + runTarget({ target: 'build', options: { WATCH: true } }, mockContext), |
| 154 | + ).toBeRejectedWithError(/Watch mode execution.*is not yet supported/); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('root-restricted host option validation', () => { |
| 158 | + let tempRoot: string; |
| 159 | + let outsideDir: string; |
| 160 | + let baseHost: MockHost; |
| 161 | + let restrictedContext: MockMcpToolContext; |
| 162 | + |
| 163 | + beforeEach(() => { |
| 164 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 165 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 166 | + tempRoot = mkdtempSync(join(baseTmpDir, 'angular-cli-run-target-root-')); |
| 167 | + outsideDir = mkdtempSync(join(baseTmpDir, 'angular-cli-run-target-outside-')); |
| 168 | + |
| 169 | + baseHost = createMockHost(); |
| 170 | + const restrictedHost = createRootRestrictedHost(baseHost, [tempRoot]); |
| 171 | + const mock = createMockContext(); |
| 172 | + addProjectToWorkspace(mock.projects, 'my-app', { |
| 173 | + build: { builder: '@angular/build:application' }, |
| 174 | + test: { builder: '@angular/build:unit-test' }, |
| 175 | + 'extract-i18n': { builder: '@angular/build:extract-i18n' }, |
| 176 | + }); |
| 177 | + |
| 178 | + const workspace = new AngularWorkspace( |
| 179 | + { projects: mock.projects, extensions: { defaultProject: 'my-app' } }, |
| 180 | + join(tempRoot, 'angular.json'), |
| 181 | + ); |
| 182 | + |
| 183 | + restrictedContext = { |
| 184 | + ...mock.context, |
| 185 | + host: restrictedHost, |
| 186 | + workspace, |
| 187 | + }; |
| 188 | + }); |
| 189 | + |
| 190 | + afterEach(() => { |
| 191 | + rmSync(tempRoot, { recursive: true, force: true }); |
| 192 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should reject and propagate out-of-root output-path in BuildTargetStrategy', async () => { |
| 196 | + await expectAsync( |
| 197 | + runTarget( |
| 198 | + { |
| 199 | + target: 'build', |
| 200 | + options: { 'output-path': outsideDir }, |
| 201 | + }, |
| 202 | + restrictedContext, |
| 203 | + ), |
| 204 | + ).toBeRejectedWithError(/Access denied: option '--output-path'/); |
| 205 | + |
| 206 | + expect(baseHost.executeNgCommand).not.toHaveBeenCalled(); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should reject and propagate out-of-root runner-config in UnitTestTargetStrategy', async () => { |
| 210 | + await expectAsync( |
| 211 | + runTarget( |
| 212 | + { |
| 213 | + target: 'test', |
| 214 | + options: { 'runner-config': join(outsideDir, 'evil.mjs') }, |
| 215 | + }, |
| 216 | + restrictedContext, |
| 217 | + ), |
| 218 | + ).toBeRejectedWithError(/Access denied: option '--runner-config'/); |
| 219 | + |
| 220 | + await expectAsync( |
| 221 | + runTarget( |
| 222 | + { |
| 223 | + target: 'test', |
| 224 | + options: { reporters: [join(outsideDir, 'evil-reporter.mjs')] }, |
| 225 | + }, |
| 226 | + restrictedContext, |
| 227 | + ), |
| 228 | + ).toBeRejectedWithError(/Access denied: option '--reporters'/); |
| 229 | + |
| 230 | + expect(baseHost.executeNgCommand).not.toHaveBeenCalled(); |
| 231 | + }); |
| 232 | + |
| 233 | + it('should reject and propagate out-of-root output-path in GenericTargetStrategy', async () => { |
| 234 | + await expectAsync( |
| 235 | + runTarget( |
| 236 | + { |
| 237 | + target: 'extract-i18n', |
| 238 | + options: { 'output-path': '../escape' }, |
| 239 | + }, |
| 240 | + restrictedContext, |
| 241 | + ), |
| 242 | + ).toBeRejectedWithError(/Access denied: option '--output-path'/); |
| 243 | + |
| 244 | + expect(baseHost.executeNgCommand).not.toHaveBeenCalled(); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should allow valid in-root output-path and base-href options', async () => { |
| 248 | + baseHost.executeNgCommand.and.resolveTo({ |
| 249 | + logs: ['Output location: dist/custom'], |
| 250 | + }); |
| 251 | + |
| 252 | + const { structuredContent } = await runTarget( |
| 253 | + { |
| 254 | + target: 'build', |
| 255 | + options: { |
| 256 | + 'output-path': 'dist/custom', |
| 257 | + 'base-href': '/my-app/', |
| 258 | + }, |
| 259 | + }, |
| 260 | + restrictedContext, |
| 261 | + ); |
| 262 | + |
| 263 | + expect(structuredContent.status).toBe('success'); |
| 264 | + expect(baseHost.executeNgCommand).toHaveBeenCalledWith( |
| 265 | + ['build', 'my-app', '--output-path=dist/custom', '--base-href=/my-app/'], |
| 266 | + { cwd: tempRoot }, |
| 267 | + ); |
| 268 | + }); |
147 | 269 | }); |
148 | 270 | }); |
0 commit comments