1818
1919import { resolve } from 'node:path' ;
2020
21+ import { env } from '@podman-desktop/api' ;
2122import { vol } from 'memfs' ;
2223import { beforeEach , expect , test , vi } from 'vitest' ;
2324
2425import { ImageCache } from './cache' ;
2526
2627vi . mock ( 'node:fs' ) ;
2728vi . mock ( 'node:fs/promises' ) ;
29+ vi . mock ( '@podman-desktop/api' ) ;
2830
2931beforeEach ( ( ) => {
32+ vi . resetAllMocks ( ) ;
3033 vol . reset ( ) ;
34+ vi . mocked ( env ) . isWindows = false ;
35+ vi . mocked ( env ) . isMac = false ;
36+ vi . mocked ( env ) . isLinux = false ;
3137} ) ;
3238
33- test ( 'images/image and images/rhel9_5 are deleted during init if they exist, rhel9_§ is not deleted' , async ( ) => {
39+ test ( 'images/image and images/rhel9_5 are deleted during init if they exist, rhel9_6 is not deleted, on Windows' , async ( ) => {
40+ vi . mocked ( env ) . isWindows = true ;
3441 const cachePath = resolve ( '/' , 'path' , 'to' , 'cache' ) ;
3542 const cache = new ImageCache ( cachePath ) ;
3643 vol . fromJSON ( {
3744 '/path/to/cache/images/image' : '' ,
38- '/path/to/cache/images/rhel9_5' : '' ,
39- '/path/to/cache/images/rhel9_6' : '' ,
45+ '/path/to/cache/images/rhel9_5.tar.gz ' : '' ,
46+ '/path/to/cache/images/rhel9_6.tar.gz ' : '' ,
4047 } ) ;
4148
4249 await cache . init ( ) ;
4350
4451 expect ( vol . toJSON ( ) ) . toEqual ( {
45- '/path/to/cache/images/rhel9_6' : '' ,
52+ '/path/to/cache/images/rhel9_6.tar.gz' : '' ,
53+ } ) ;
54+ } ) ;
55+
56+ test ( 'images/image and images/rhel9_5 are deleted during init if they exist, rhel9_6 is not deleted, on Mac' , async ( ) => {
57+ vi . mocked ( env ) . isMac = true ;
58+ const cachePath = resolve ( '/' , 'path' , 'to' , 'cache' ) ;
59+ const cache = new ImageCache ( cachePath ) ;
60+ vol . fromJSON ( {
61+ '/path/to/cache/images/image' : '' ,
62+ '/path/to/cache/images/rhel9_5.tar.gz' : '' ,
63+ '/path/to/cache/images/rhel9_6.qcow2' : '' ,
64+ } ) ;
65+
66+ await cache . init ( ) ;
67+
68+ expect ( vol . toJSON ( ) ) . toEqual ( {
69+ '/path/to/cache/images/rhel9_6.qcow2' : '' ,
4670 } ) ;
4771} ) ;
4872
@@ -56,11 +80,28 @@ test('images directory is created', async () => {
5680 } ) ;
5781} ) ;
5882
59- test ( 'getPath returns path for known image' , async ( ) => {
83+ test ( 'getPath returns path for known image on Windows' , async ( ) => {
84+ vi . mocked ( env ) . isWindows = true ;
85+ const cache = new ImageCache ( resolve ( '/' , 'path' , 'to' , 'cache' ) ) ;
86+ await cache . init ( ) ;
87+ const path = cache . getPath ( 'RHEL 10.0' ) ;
88+ expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10_0.tar.gz' ) ) ;
89+ } ) ;
90+
91+ test ( 'getPath returns path for known image on Linux' , async ( ) => {
92+ vi . mocked ( env ) . isMac = true ;
93+ const cache = new ImageCache ( resolve ( '/' , 'path' , 'to' , 'cache' ) ) ;
94+ await cache . init ( ) ;
95+ const path = cache . getPath ( 'RHEL 10.0' ) ;
96+ expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10_0.qcow2' ) ) ;
97+ } ) ;
98+
99+ test ( 'getPath returns path for known image on Mac' , async ( ) => {
100+ vi . mocked ( env ) . isMac = true ;
60101 const cache = new ImageCache ( resolve ( '/' , 'path' , 'to' , 'cache' ) ) ;
61102 await cache . init ( ) ;
62103 const path = cache . getPath ( 'RHEL 10.0' ) ;
63- expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10_0' ) ) ;
104+ expect ( path ) . toBe ( resolve ( '/' , 'path' , 'to' , 'cache' , 'images' , 'rhel10_0.qcow2 ' ) ) ;
64105} ) ;
65106
66107test ( 'getPath raises an exception for an unknown image' , async ( ) => {
0 commit comments