1616 * SPDX-License-Identifier: Apache-2.0
1717 ***********************************************************************/
1818
19- import * as fs from 'node:fs' ;
19+ import type { Dirent } from 'node:fs' ;
2020import * as fsPromises from 'node:fs/promises' ;
2121import { resolve } from 'node:path' ;
2222
@@ -27,15 +27,32 @@ import { ImageCache } from './cache';
2727vi . mock ( 'node:fs' ) ;
2828vi . mock ( 'node:fs/promises' ) ;
2929
30- test ( 'images/image file is deleted during init if it exists' , async ( ) => {
31- const cache = new ImageCache ( resolve ( '/' , 'path' , 'to' , 'cache' ) ) ;
30+ test ( 'images/image and images/rhel9_5 are deleted during init if they exist, rhel9_§ is not deleted' , async ( ) => {
31+ const cachePath = resolve ( '/' , 'path' , 'to' , 'cache' ) ;
32+ const cache = new ImageCache ( cachePath ) ;
3233
33- vi . mocked ( fs . existsSync ) . mockImplementation ( ( path : fs . PathLike ) : boolean => {
34- return path === resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'image' ) ;
35- } ) ;
34+ vi . mocked ( fsPromises . readdir ) . mockResolvedValue ( [
35+ {
36+ name : 'image' ,
37+ parentPath : resolve ( cachePath , 'images' ) ,
38+ isFile : ( ) => true ,
39+ } as Dirent ,
40+ {
41+ name : 'rhel9_5' ,
42+ parentPath : resolve ( cachePath , 'images' ) ,
43+ isFile : ( ) => true ,
44+ } as Dirent ,
45+ {
46+ name : 'rhel9_6' ,
47+ parentPath : resolve ( cachePath , 'images' ) ,
48+ isFile : ( ) => true ,
49+ } as Dirent ,
50+ ] ) ;
3651 await cache . init ( ) ;
3752
3853 expect ( fsPromises . rm ) . toHaveBeenCalledWith ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'image' ) ) ;
54+ expect ( fsPromises . rm ) . toHaveBeenCalledWith ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel9_5' ) ) ;
55+ expect ( fsPromises . rm ) . not . toHaveBeenCalledWith ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel9_6' ) ) ;
3956} ) ;
4057
4158test ( 'images directory is created' , async ( ) => {
@@ -49,8 +66,8 @@ test('images directory is created', async () => {
4966test ( 'getPath returns path for known image' , async ( ) => {
5067 const cache = new ImageCache ( resolve ( '/' , 'path' , 'to' , 'cache' ) ) ;
5168 await cache . init ( ) ;
52- const path = cache . getPath ( 'RHEL 10' ) ;
53- expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10 ' ) ) ;
69+ const path = cache . getPath ( 'RHEL 10.0 ' ) ;
70+ expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10_0 ' ) ) ;
5471} ) ;
5572
5673test ( 'getPath raises an exception for an unknown image' , async ( ) => {
0 commit comments