@@ -28,6 +28,7 @@ public async Task With_UpdateHelperServiceMock_ShouldWorkProperly()
2828
2929 mockUpdateHelperService . Setup ( u => u . GetApplicationBaseDirectory ( ) ) . Returns ( testDirectoryService . TestDirectory ) ;
3030
31+ // Create test files and backup files
3132 testDirectoryService . CreateSubTestFile ( "file1.txt" , "file1Content" ) ;
3233 testDirectoryService . CreateSubTestFile ( "file2.txt" , "file2Content" ) ;
3334 testDirectoryService . CreateSubTestFile ( "file3.txt" , "file3Content" ) ;
@@ -50,49 +51,97 @@ public async Task With_UpdateHelperServiceMock_ShouldWorkProperly()
5051 files . Count ( f => f . Name . StartsWith ( "file3." ) ) . Should ( ) . Be ( 1 ) ;
5152 files . Count ( f => f . Name . StartsWith ( "file7." ) ) . Should ( ) . Be ( 1 ) ;
5253 }
53-
54+
5455 [ Test ]
55- public async Task On_WindowsOrLinux_ShouldWorkProperly ( )
56+ public async Task With_UpdateHelperServiceMock_ShouldNotAffectSubDirectories ( )
5657 {
57- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) && ! RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
58- {
59- Assert . Ignore ( "This test is only for Windows and Linux" ) ;
60- }
61-
6258 // Arrange
63- RegisterType < UpdateHelperService , IUpdateHelperService > ( ) ;
6459 RegisterType < DeleteUpdateBackupSnippetsService > ( ) ;
6560 BuildMoqContainer ( ) ;
66-
67- var contextHelper = new TestContextGenerator ( Container ) ;
61+
6862 _testDirectoryService . CreateTestDirectory ( ) ;
69-
70- var environmentService = Container . Resolve < Mock < IEnvironmentService > > ( ) ;
63+
64+ var mockUpdateHelperService = Container . Resolve < Mock < IUpdateHelperService > > ( ) ;
7165 var testDirectoryService = Container . Resolve < ITestDirectoryService > ( ) ;
7266 var deleteUpdateBackupSnippetsService = Container . Resolve < DeleteUpdateBackupSnippetsService > ( ) ;
67+
68+ mockUpdateHelperService . Setup ( u => u . GetApplicationBaseDirectory ( ) ) . Returns ( testDirectoryService . TestDirectory ) ;
7369
74- environmentService . Setup ( e => e . AssemblyFullName ) . Returns ( testDirectoryService . TestDirectory . FullName ) ;
75-
70+ // Create test files, subdirectories, and backup files
7671 testDirectoryService . CreateSubTestFile ( "file1.txt" , "file1Content" ) ;
77- testDirectoryService . CreateSubTestFile ( "file2.txt" , "file2Content" ) ;
78- testDirectoryService . CreateSubTestFile ( "file3.txt" , "file3Content" ) ;
72+ testDirectoryService . CreateSubTestFile ( $ "file_to_delete.{ UpdateConstants . BAK_EXTENSION } 0", "fileContent" ) ;
7973
80- testDirectoryService . CreateSubTestFile ( $ "file4.txt.{ UpdateConstants . BAK_EXTENSION } 0", "file4Content" ) ;
81- testDirectoryService . CreateSubTestFile ( $ "file5.txt.{ UpdateConstants . BAK_EXTENSION } 1", "file5Content" ) ;
82- testDirectoryService . CreateSubTestFile ( $ "file6.txt.{ UpdateConstants . BAK_EXTENSION } 15", "file6Content" ) ;
83- testDirectoryService . CreateSubTestFile ( $ "file7.txt.{ UpdateConstants . BAK_EXTENSION } 15.ext", "file7Content" ) ;
74+ var subDir = testDirectoryService . TestDirectory . CreateSubdirectory ( "subdir" ) ;
75+ await File . WriteAllTextAsync ( Path . Combine ( subDir . FullName , "subdir_file.txt" ) , "normalContent" ) ;
76+ await File . WriteAllTextAsync ( Path . Combine ( subDir . FullName , $ "subdir_backup.{ UpdateConstants . BAK_EXTENSION } 0") , "backupContent" ) ;
77+
78+ var backupDir = testDirectoryService . TestDirectory . CreateSubdirectory ( $ "dir.{ UpdateConstants . BAK_EXTENSION } 1") ;
79+ await File . WriteAllTextAsync ( Path . Combine ( backupDir . FullName , "file_in_backup_dir.txt" ) , "fileContent" ) ;
8480
8581 // Act
8682 await deleteUpdateBackupSnippetsService . DeleteBackupSnippetsAsync ( ) ;
87-
83+
8884 // Assert
8985 testDirectoryService . TestDirectory . Refresh ( ) ;
90- var files = testDirectoryService . TestDirectory . GetFiles ( "*" , SearchOption . AllDirectories ) ;
9186
92- files . Length . Should ( ) . Be ( 4 ) ;
93- files . Count ( f => f . Name . StartsWith ( "file1." ) ) . Should ( ) . Be ( 1 ) ;
94- files . Count ( f => f . Name . StartsWith ( "file2." ) ) . Should ( ) . Be ( 1 ) ;
95- files . Count ( f => f . Name . StartsWith ( "file3." ) ) . Should ( ) . Be ( 1 ) ;
96- files . Count ( f => f . Name . StartsWith ( "file7." ) ) . Should ( ) . Be ( 1 ) ;
87+ File . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , "file1.txt" ) ) . Should ( ) . BeTrue ( ) ;
88+ File . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , $ "file_to_delete.{ UpdateConstants . BAK_EXTENSION } 0") ) . Should ( ) . BeFalse ( ) ;
89+ Directory . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , $ "dir.{ UpdateConstants . BAK_EXTENSION } 1") ) . Should ( ) . BeFalse ( ) ;
90+ Directory . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , "subdir" ) ) . Should ( ) . BeTrue ( ) ;
91+ File . Exists ( Path . Combine ( subDir . FullName , "subdir_file.txt" ) ) . Should ( ) . BeTrue ( ) ;
92+ File . Exists ( Path . Combine ( subDir . FullName , $ "subdir_backup.{ UpdateConstants . BAK_EXTENSION } 0") ) . Should ( ) . BeTrue ( ) ;
93+ }
94+
95+ [ Test ]
96+ public async Task With_UpdateHelperServiceMock_ShouldDeleteUpdateUnzipDirectoriesAtRootOnly ( )
97+ {
98+ // Arrange
99+ RegisterType < DeleteUpdateBackupSnippetsService > ( ) ;
100+ BuildMoqContainer ( ) ;
101+
102+ _testDirectoryService . CreateTestDirectory ( ) ;
103+
104+ var mockUpdateHelperService = Container . Resolve < Mock < IUpdateHelperService > > ( ) ;
105+ var testDirectoryService = Container . Resolve < ITestDirectoryService > ( ) ;
106+ var deleteUpdateBackupSnippetsService = Container . Resolve < DeleteUpdateBackupSnippetsService > ( ) ;
107+
108+ mockUpdateHelperService . Setup ( u => u . GetApplicationBaseDirectory ( ) ) . Returns ( testDirectoryService . TestDirectory ) ;
109+
110+ // Create test files and directories
111+ testDirectoryService . CreateSubTestFile ( "normal.txt" , "normalContent" ) ;
112+
113+ // Update extraction directory at the root (should be deleted)
114+ var rootExtractDir = testDirectoryService . TestDirectory . CreateSubdirectory ( $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _123") ;
115+ await File . WriteAllTextAsync ( Path . Combine ( rootExtractDir . FullName , "extract.txt" ) , "extractContent" ) ;
116+
117+ // Normal directory at the root
118+ var normalDir = testDirectoryService . TestDirectory . CreateSubdirectory ( "normalDir" ) ;
119+
120+ // Normal file in a subdirectory
121+ await File . WriteAllTextAsync ( Path . Combine ( normalDir . FullName , "subdir_file.txt" ) , "normalContent" ) ;
122+
123+ // Update extraction directory in a subdirectory (should not be deleted)
124+ var subExtractDir = normalDir . CreateSubdirectory ( $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _456") ;
125+ await File . WriteAllTextAsync ( Path . Combine ( subExtractDir . FullName , "sub_extract.txt" ) , "subExtractContent" ) ;
126+
127+ // File with extraction pattern in a subdirectory (should not be deleted)
128+ await File . WriteAllTextAsync (
129+ Path . Combine ( normalDir . FullName , $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _file.txt") ,
130+ "extractNamedContent"
131+ ) ;
132+
133+ // Act
134+ await deleteUpdateBackupSnippetsService . DeleteBackupSnippetsAsync ( ) ;
135+
136+ // Assert
137+ testDirectoryService . TestDirectory . Refresh ( ) ;
138+
139+ File . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , "normal.txt" ) ) . Should ( ) . BeTrue ( ) ;
140+ Directory . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _123") ) . Should ( ) . BeFalse ( ) ;
141+ Directory . Exists ( Path . Combine ( testDirectoryService . TestDirectory . FullName , "normalDir" ) ) . Should ( ) . BeTrue ( ) ;
142+ File . Exists ( Path . Combine ( normalDir . FullName , "subdir_file.txt" ) ) . Should ( ) . BeTrue ( ) ;
143+ Directory . Exists ( Path . Combine ( normalDir . FullName , $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _456") ) . Should ( ) . BeTrue ( ) ;
144+ File . Exists ( Path . Combine ( subExtractDir . FullName , "sub_extract.txt" ) ) . Should ( ) . BeTrue ( ) ;
145+ File . Exists ( Path . Combine ( normalDir . FullName , $ "{ UpdateConstants . UPDATE_UNZIP_EXTRACT_START_NAME } _file.txt") ) . Should ( ) . BeTrue ( ) ;
97146 }
98- }
147+ }
0 commit comments