1+ import t from "tap" ;
2+ import { mustache } from "../lib/mustache.ts" ;
3+
4+ void t . test ( "mustache" , async ( t ) => {
5+ await t . test ( "must include a path" , ( t ) => {
6+ // @ts -expect-error bad data on purpose
7+ t . throws ( ( ) => mustache ( { } ) ) ;
8+ t . end ( ) ;
9+ } ) ;
10+
11+ await t . test ( "should generate" , async ( t ) => {
12+ const { mustache } = t . mockRequire < typeof import ( "../lib/mustache.ts" ) > ( "../lib/mustache.ts" , {
13+ "node:fs/promises" : {
14+ readFile : ( path : string ) => {
15+ t . equal ( path , "path/to/file.txt" ) ;
16+ return Promise . resolve ( "<% title %>" ) ;
17+ }
18+ }
19+ } ) ;
20+ const variables = {
21+ title : "it generates"
22+ } ;
23+
24+ const generator = mustache ( {
25+ path : "path/to/file.txt" ,
26+ variables,
27+ } ) ;
28+
29+ const output = await generator . generate ( ) ;
30+ t . equal ( output , "it generates" ) ;
31+ } ) ;
32+
33+ await t . test ( "should validate partials" , async ( t ) => {
34+ const { mustache } = t . mockRequire < typeof import ( "../lib/mustache.ts" ) > ( "../lib/mustache.ts" , {
35+ "node:fs/promises" : {
36+ readFile : ( ) => Promise . resolve ( "<% title %>" )
37+ }
38+ } ) ;
39+ const variables = {
40+ title : "it generates"
41+ } ;
42+
43+ const generator = mustache ( {
44+ path : "path/to/file.txt" ,
45+ variables,
46+ } ) ;
47+
48+ const generateSpy = t . sinon . spy ( generator , "generate" ) ;
49+ const reportSpy = t . sinon . spy ( generator , "report" ) ;
50+
51+ await generator . validate ( {
52+ path : "path/to/file.txt" ,
53+ found : "it generates\nand handles extras"
54+ } ) ;
55+
56+ t . equal ( generateSpy . callCount , 1 ) ;
57+ // Report not called because we passed
58+ t . equal ( reportSpy . callCount , 0 ) ;
59+ } ) ;
60+
61+ await t . test ( "should fail validation when base template not found" , async ( t ) => {
62+ const { mustache } = t . mockRequire < typeof import ( "../lib/mustache.ts" ) > ( "../lib/mustache.ts" , {
63+ "node:fs/promises" : {
64+ readFile : ( ) => Promise . resolve ( "<% title %>" )
65+ }
66+ } ) ;
67+ const variables = {
68+ title : "it generates"
69+ } ;
70+
71+ const generator = mustache ( {
72+ path : "path/to/file.txt" ,
73+ variables,
74+ } ) ;
75+
76+ const generateSpy = t . sinon . spy ( generator , "generate" ) ;
77+ const reportSpy = t . sinon . spy ( generator , "report" ) ;
78+
79+ await generator . validate ( {
80+ path : "path/to/file.txt" ,
81+ found : "a new file"
82+ } ) ;
83+
84+ t . equal ( generateSpy . callCount , 1 ) ;
85+ t . ok ( reportSpy . calledOnceWithExactly ( {
86+ expected : "it generates" ,
87+ found : "a new file" ,
88+ message : "path/to/file.txt does not include the original template"
89+ } ) ) ;
90+ } ) ;
91+ } ) ;
0 commit comments