@@ -309,7 +309,8 @@ describe('CLI', () => {
309309 const stdoutRatio = grepTotalRatio ( stdout ) ;
310310
311311 expectStringContains ( stdout , 'Converting 1 image (lossy)...' ) ;
312- expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .png` ) ) ;
312+ expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .avif` ) ) ;
313+ expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .webp` ) ) ;
313314 expectRatio ( stdoutRatio , 85 , 90 ) ;
314315 expectFileNotModified ( `${ fileBasename } .png` ) ;
315316 expectFileExists ( `${ fileBasename } .avif` ) ;
@@ -324,7 +325,8 @@ describe('CLI', () => {
324325 const stdoutRatio = grepTotalRatio ( stdout ) ;
325326
326327 expectStringContains ( stdout , 'Converting 1 image (lossless)...' ) ;
327- expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .png` ) ) ;
328+ expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .avif` ) ) ;
329+ expectStringContains ( stdout , path . join ( temporary , `${ fileBasename } .webp` ) ) ;
328330 expectRatio ( stdoutRatio , 35 , 40 ) ;
329331 expectFileNotModified ( `${ fileBasename } .png` ) ;
330332 expectFileExists ( `${ fileBasename } .avif` ) ;
@@ -413,6 +415,46 @@ describe('CLI', () => {
413415 } ) ;
414416 } ) ;
415417
418+ describe ( 'Prefix and Suffix (--prefix --suffix)' , ( ) => {
419+ let outputDirectory ;
420+
421+ beforeEach ( ( ) => {
422+ outputDirectory = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'optimizt-test-' ) ) ;
423+ } ) ;
424+
425+ afterEach ( ( ) => {
426+ if ( outputDirectory ) {
427+ fs . rmSync ( outputDirectory , { recursive : true } ) ;
428+ }
429+ } ) ;
430+
431+ test ( 'Should add prefix and suffix to optimized filenames' , ( ) => {
432+ const fileName = 'png-not-optimized.png' ;
433+ const expectedOutput = 'prepng-not-optimizedsuf.png' ;
434+
435+ runCliWithParameters ( `--prefix pre --suffix suf --output ${ outputDirectory } ${ workDirectory } ${ fileName } ` ) ;
436+ expect ( fs . existsSync ( path . join ( outputDirectory , expectedOutput ) ) ) . toBeTruthy ( ) ;
437+ } ) ;
438+
439+ test ( 'Should add prefix and suffix to converted filenames' , ( ) => {
440+ const fileBasename = 'png-not-optimized' ;
441+ const expectedAvif = 'prepng-not-optimizedsuf.avif' ;
442+ const expectedWebp = 'prepng-not-optimizedsuf.webp' ;
443+
444+ runCliWithParameters ( `--avif --webp --prefix pre --suffix suf --output ${ outputDirectory } ${ workDirectory } ${ fileBasename } .png` ) ;
445+ expect ( fs . existsSync ( path . join ( outputDirectory , expectedAvif ) ) ) . toBeTruthy ( ) ;
446+ expect ( fs . existsSync ( path . join ( outputDirectory , expectedWebp ) ) ) . toBeTruthy ( ) ;
447+ } ) ;
448+
449+ test ( 'Should sanitize forbidden characters in prefix and suffix' , ( ) => {
450+ const fileName = 'png-not-optimized.png' ;
451+ const expectedOutput = 'unsafepng-not-optimizedunsafe.png' ;
452+
453+ runCliWithParameters ( `--prefix "<un:safe>" --suffix "<un:safe>" --output ${ outputDirectory } ${ workDirectory } ${ fileName } ` ) ;
454+ expect ( fs . existsSync ( path . join ( outputDirectory , expectedOutput ) ) ) . toBeTruthy ( ) ;
455+ } ) ;
456+ } ) ;
457+
416458 describe ( 'Help (--help)' , ( ) => {
417459 const helpString = `\
418460Usage: cli [options] <dir> <file ...>
@@ -428,6 +470,8 @@ Options:
428470 -c, --config <path> use this configuration, overriding default config options
429471 if present
430472 -o, --output <path> write output to directory
473+ -p, --prefix <text> add prefix to optimized file names
474+ -s, --suffix <text> add suffix to optimized file names
431475 -V, --version output the version number
432476 -h, --help display help for command
433477` ;
@@ -501,8 +545,6 @@ function expectRatio(current, min, max) {
501545}
502546
503547function expectFileRatio ( { file, maxRatio, minRatio, stdout, outputExt } ) {
504- expectStringContains ( stdout , path . join ( temporary , file ) ) ;
505-
506548 const fileBasename = path . basename ( file , path . extname ( file ) ) ;
507549 const outputFile = outputExt ? `${ fileBasename } .${ outputExt } ` : file ;
508550
@@ -512,6 +554,7 @@ function expectFileRatio({ file, maxRatio, minRatio, stdout, outputExt }) {
512554 const calculatedRatio = calculateRatio ( sizeBefore , sizeAfter ) ;
513555 const stdoutRatio = grepTotalRatio ( stdout ) ;
514556
557+ expectStringContains ( stdout , path . join ( temporary , outputFile ) ) ;
515558 expect ( stdoutRatio ) . toBe ( calculatedRatio ) ;
516559 expectRatio ( stdoutRatio , minRatio , maxRatio ) ;
517560}
0 commit comments