You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(docs-infra): convert hard-coded cli-builder examples into a proper mini-app (angular#34362)
Previously, the examples in the `cli-builder` guide were hard-coded.
This made it impossible to test them and verify they are correct.
This commit fixes this by converting them into a proper mini-app. In a
subsequent commit, tests will be added to verify that the source code
works as expected (and guard against regressions).
Fixesangular#34314
PR Closeangular#34362
@@ -486,73 +419,21 @@ Because we did not override the *args* option, it will list information about th
486
419
487
420
Use integration testing for your builder, so that you can use the Architect scheduler to create a context, as in this [example](https://github.com/mgechev/cli-builders-demo).
488
421
489
-
* In the builder source directory, we have created a new test file `index.spec.ts`. The code creates new instances of `JsonSchemaRegistry` (for schema validation), `TestingArchitectHost` (an in-memory implementation of `ArchitectHost`), and `Architect`.
422
+
* In the builder source directory, we have created a new test file `my-builder.spec.ts`. The code creates new instances of `JsonSchemaRegistry` (for schema validation), `TestingArchitectHost` (an in-memory implementation of `ArchitectHost`), and `Architect`.
490
423
491
424
* We've added a `builders.json` file next to the builder's [`package.json` file](https://github.com/mgechev/cli-builders-demo/blob/master/command-builder/builders.json), and modified the package file to point to it.
492
425
493
426
Here’s an example of a test that runs the command builder.
494
-
The test uses the builder to run the `ls` command, then validates that it ran successfully and listed the proper files.
// TestingArchitectHost() takes workspace and current directories.
512
-
// Since we don't use those, both are the same in this case.
513
-
architectHost = new TestingArchitectHost(__dirname, __dirname);
514
-
architect = new Architect(architectHost, registry);
515
-
516
-
// This will either take a Node package name, or a path to the directory
517
-
// for the package.json file.
518
-
await architectHost.addBuilderFromPackage('..');
519
-
});
520
-
521
-
// This might not work in Windows.
522
-
it('can run ls', async () => {
523
-
// Create a logger that keeps an array of all messages that were logged.
524
-
const logger = new logging.Logger('');
525
-
const logs = [];
526
-
logger.subscribe(ev => logs.push(ev.message));
527
-
528
-
// A "run" can have multiple outputs, and contains progress information.
529
-
const run = await architect.scheduleBuilder('@example/command-runner:command', {
530
-
command: 'ls',
531
-
args: [__dirname],
532
-
}, { logger }); // We pass the logger for checking later.
533
-
534
-
// The "result" member (of type BuilderOutput) is the next output.
535
-
const output = await run.result;
536
-
537
-
// Stop the builder from running. This stops Architect from keeping
538
-
// the builder-associated states in memory, since builders keep waiting
539
-
// to be scheduled.
540
-
await run.stop();
541
-
542
-
// Expect that it succeeded.
543
-
expect(output.success).toBe(true);
544
-
545
-
// Expect that this file was listed. It should be since we're running
546
-
// `ls $__dirname`.
547
-
expect(logs).toContain('index.spec.ts');
548
-
});
549
-
});
427
+
The test uses the builder to run the `node --print 'foo'` command, then validates that the `logger` contains an entry for `foo`.
550
428
429
+
<code-example
430
+
path="cli-builder/src/my-builder.spec.ts"
431
+
header="src/my-builder.spec.ts">
551
432
</code-example>
552
433
553
434
<divclass="alert is-helpful">
554
435
555
-
When running this test in your repo, you need the [`ts-node`](https://github.com/TypeStrong/ts-node) package. You can avoid this by renaming `index.spec.ts` to `index.spec.js`.
436
+
When running this test in your repo, you need the [`ts-node`](https://github.com/TypeStrong/ts-node) package. You can avoid this by renaming `my-builder.spec.ts` to `my-builder.spec.js`.
0 commit comments