@@ -430,6 +430,9 @@ const planConfig = object({
430430 } ,
431431} ) ;
432432
433+ const TEST_REPORTER_OPTION = "--reporter" ;
434+ const testReporter = z . enum ( [ "pretty" , "failures" ] ) ;
435+
433436const testConfig = object ( {
434437 path : {
435438 description : "markdown document or directory to test (defaults to the current directory)" ,
@@ -439,6 +442,10 @@ const testConfig = object({
439442 description : "glob for test documents, relative to a directory target (repeatable)" ,
440443 ...field ( z . array ( z . string ( ) ) , field . default ( [ "**/*.test.md" ] ) , field . array ( ) ) ,
441444 } ,
445+ reporter : {
446+ description : "test output: pretty (default) or failures" ,
447+ ...field ( testReporter , field . default ( "pretty" ) ) ,
448+ } ,
442449 include : {
443450 description : "component search directory" ,
444451 ...field ( z . array ( z . string ( ) ) , field . default ( [ "components" , "." ] ) , field . array ( ) ) ,
@@ -1776,17 +1783,23 @@ type TestPlugins = (document: string) => Operation<CommandPlugins>;
17761783/**
17771784 * `xmd test` — one document, or every document a directory holds.
17781785 *
1779- * A directory keeps going after a failure and decides the status once at the
1780- * end. A single document behaves exactly as it always has: one reported
1781- * failure, no heading, no summary.
1786+ * A directory runs every matching document and settles the exit status once.
17821787 */
17831788function * test (
17841789 config : TestConfig ,
17851790 args : string [ ] ,
17861791 installService : HostServiceInstaller ,
17871792 testPlugins : TestPlugins ,
17881793) : Operation < void > {
1789- const patterns = readPatternFlags ( args ) ;
1794+ const reporterFlags = readTestOption ( args , TEST_REPORTER_OPTION ) ;
1795+ const reporter = testReporter . safeParse ( reporterFlags . values [ 0 ] ?? "pretty" ) ;
1796+ if ( reporterFlags . missingValue || reporterFlags . values . length > 1 || ! reporter . success ) {
1797+ console . error ( "--reporter accepts one value: pretty or failures" ) ;
1798+ yield * exit ( 1 ) ;
1799+ return ;
1800+ }
1801+ const failuresOnly = reporter . data === "failures" ;
1802+ const patterns = readTestOption ( args , PATTERN_OPTION ) ;
17901803 if ( patterns . missingValue ) {
17911804 console . error (
17921805 `${ PATTERN_OPTION } requires a value — write \`${ PATTERN_OPTION } <glob>\`, or ` +
@@ -1814,14 +1827,27 @@ function* test(
18141827 return ;
18151828 }
18161829 announceSecretDetection ( config . secretDetection ) ;
1817- const { value : result , delivery } = yield * withTestProgress ( ( diagnostics ) =>
1818- runTestDocument ( { ...config , root : { path } } , testPlugins , installService , diagnostics ) ,
1830+ const {
1831+ value : result ,
1832+ delivery,
1833+ summary,
1834+ } = yield * withTestProgress ( failuresOnly , ( diagnostics ) =>
1835+ runTestDocument (
1836+ { ...config , root : { path } , discardOutput : failuresOnly } ,
1837+ testPlugins ,
1838+ installService ,
1839+ diagnostics ,
1840+ ) ,
18191841 ) ;
18201842 if ( ! result . ok ) {
18211843 reportFailure ( result . error ) ;
1822- yield * exit ( 1 ) ;
18231844 } else if ( ! delivery . ok ) {
18241845 reportFailure ( delivery . error ) ;
1846+ }
1847+ if ( failuresOnly ) {
1848+ console . error ( summary ) ;
1849+ }
1850+ if ( ! result . ok || ! delivery . ok ) {
18251851 yield * exit ( 1 ) ;
18261852 }
18271853 return ;
@@ -1847,15 +1873,22 @@ function* test(
18471873 // and a directory of fifty documents would otherwise say so fifty times.
18481874 announceSecretDetection ( config . secretDetection ) ;
18491875
1850- const { value : failures , delivery } = yield * withTestProgress ( function * ( diagnostics ) {
1876+ const {
1877+ value : failures ,
1878+ delivery,
1879+ summary,
1880+ } = yield * withTestProgress ( failuresOnly , function * ( diagnostics ) {
18511881 const failed : string [ ] = [ ] ;
18521882 for ( const document of target . documents ) {
1853- process . stdout . write ( `\n# ${ document . relativePath } \n\n` ) ;
1883+ if ( ! failuresOnly ) {
1884+ process . stdout . write ( `\n# ${ document . relativePath } \n\n` ) ;
1885+ }
18541886 const result = yield * runTestDocument (
18551887 {
18561888 ...config ,
18571889 root : { path : document . path } ,
18581890 include : componentSearchPath ( document , target . root , config . include ) ,
1891+ discardOutput : failuresOnly ,
18591892 } ,
18601893 testPlugins ,
18611894 installService ,
@@ -1869,11 +1902,19 @@ function* test(
18691902 return failed ;
18701903 } ) ;
18711904
1872- if ( failures . length > 0 ) {
1873- console . error ( `\n${ failures . length } of ${ target . documents . length } documents failed` ) ;
1874- yield * exit ( 1 ) ;
1875- } else if ( ! delivery . ok ) {
1905+ const failedDocuments =
1906+ failures . length > 0
1907+ ? `${ failures . length } of ${ target . documents . length } documents failed`
1908+ : undefined ;
1909+ if ( failedDocuments === undefined && ! delivery . ok ) {
18761910 reportFailure ( delivery . error ) ;
1911+ }
1912+ if ( failuresOnly ) {
1913+ console . error ( failedDocuments === undefined ? summary : `${ summary } · ${ failedDocuments } ` ) ;
1914+ } else if ( failedDocuments !== undefined ) {
1915+ console . error ( `\n${ failedDocuments } ` ) ;
1916+ }
1917+ if ( failures . length > 0 || ! delivery . ok ) {
18771918 yield * exit ( 1 ) ;
18781919 }
18791920}
@@ -1886,15 +1927,22 @@ function* test(
18861927 * returns, so whatever the command reports next follows them.
18871928 */
18881929function * withTestProgress < T > (
1930+ failuresOnly : boolean ,
18891931 body : ( diagnostics : TestDiagnostics ) => Operation < T > ,
1890- ) : Operation < { value : T ; delivery : Result < void > } > {
1891- let delivery : ( ) => Result < void > = ( ) => Ok ( undefined ) ;
1892- const value = yield * scoped ( function * ( ) {
1893- const diagnostics = yield * useTestDiagnostics ( process . stderr ) ;
1894- delivery = diagnostics . delivery ;
1895- return yield * body ( diagnostics ) ;
1932+ ) : Operation < { value : T ; delivery : Result < void > ; summary : string } > {
1933+ const started = performance . now ( ) ;
1934+ const run = yield * scoped ( function * ( ) {
1935+ const diagnostics = yield * useTestDiagnostics ( process . stderr , failuresOnly ) ;
1936+ const value = yield * body ( diagnostics ) ;
1937+ return { value , diagnostics } ;
18961938 } ) ;
1897- return { value, delivery : delivery ( ) } ;
1939+ const { pass, fail } = run . diagnostics . results ( ) ;
1940+ const elapsed = Math . round ( performance . now ( ) - started ) ;
1941+ return {
1942+ value : run . value ,
1943+ delivery : run . diagnostics . delivery ( ) ,
1944+ summary : `[xmd test] ${ pass } passed | ${ fail } failed (${ elapsed } ms)` ,
1945+ } ;
18981946}
18991947
19001948/**
@@ -2002,15 +2050,13 @@ function findPropsFlag(args: string[]): string | undefined {
20022050
20032051const PATTERN_OPTION = "--pattern" ;
20042052
2005- interface PatternFlags {
2006- /** Values the caller wrote, in the order they wrote them. */
2053+ interface TestOption {
20072054 values : string [ ] ;
2008- /** A `--pattern` that ran out of argv or was followed by another option. */
20092055 missingValue : boolean ;
20102056}
20112057
20122058/**
2013- * Read `--pattern` from argv.
2059+ * Read an explicitly supplied test option from argv.
20142060 *
20152061 * The resolved configuration answers none of the questions this serves. It
20162062 * cannot say whether the option was given at all — the default is a real
@@ -2021,15 +2067,15 @@ interface PatternFlags {
20212067 * A separated value that begins with `-` is another option, not a glob;
20222068 * `--pattern=<glob>` expresses a glob that really does begin with one.
20232069 */
2024- function readPatternFlags ( args : string [ ] ) : PatternFlags {
2070+ function readTestOption ( args : string [ ] , option : string ) : TestOption {
20252071 const values : string [ ] = [ ] ;
20262072 let missingValue = false ;
20272073
20282074 for ( const [ index , arg ] of args . entries ( ) ) {
20292075 if ( arg === "--" ) {
20302076 break ;
20312077 }
2032- if ( arg === PATTERN_OPTION ) {
2078+ if ( arg === option ) {
20332079 const value = args [ index + 1 ] ;
20342080 if ( value === undefined || value . startsWith ( "-" ) ) {
20352081 missingValue = true ;
@@ -2038,8 +2084,8 @@ function readPatternFlags(args: string[]): PatternFlags {
20382084 values . push ( value ) ;
20392085 continue ;
20402086 }
2041- if ( arg . startsWith ( `${ PATTERN_OPTION } =` ) ) {
2042- values . push ( arg . slice ( PATTERN_OPTION . length + 1 ) ) ;
2087+ if ( arg . startsWith ( `${ option } =` ) ) {
2088+ values . push ( arg . slice ( option . length + 1 ) ) ;
20432089 }
20442090 }
20452091
0 commit comments