55import pkgfile_test
66import os
77from collections import defaultdict
8+ from pathlib import Path
89
910
1011def _sha256 (path ):
@@ -23,12 +24,12 @@ def setUp(self):
2324
2425 # relocate our cachedir to not overwrite the golden fileset
2526 self .goldendir = self .cachedir
26- self .cachedir = os . path . join (self .tempdir , 'cache' )
27- os . mkdir ( self .cachedir )
27+ self .cachedir = Path (self .tempdir , 'cache' )
28+ self .cachedir . mkdir ( )
2829
2930 @staticmethod
3031 def getRepoFiles (subdir , reponame ):
31- return sorted (glob .glob ('{}/{} .files.*'. format ( subdir , reponame ) ))
32+ return sorted (Path ( subdir ) .glob (f' { reponame } .files.*' ))
3233
3334 def assertMatchesGolden (self , reponame ):
3435 golden_files = self .getRepoFiles (self .goldendir , reponame )
@@ -48,15 +49,14 @@ def testUpdate(self):
4849 self .assertMatchesGolden ('testing' )
4950
5051 for repo in ('multilib' , 'testing' ):
51- original_repo = '{}/x86_64/{repo}/{repo}.files' .format (
52- self .alpmcachedir , repo = repo )
52+ original_repo = Path (self .alpmcachedir , 'x86_64' , repo , f'{ repo } .files' )
5353
5454 for converted_repo in self .getRepoFiles (self .cachedir , repo ):
5555 # Only compare the integer portion of the mtime. we'll only ever
5656 # get back second precision from a remote server, so any fractional
5757 # second that's present on our golden repo can be ignored.
58- self .assertEqual (int (os .stat (original_repo ).st_mtime ),
59- int (os .stat (converted_repo ).st_mtime ))
58+ self .assertEqual (int (original_repo .stat ().st_mtime ),
59+ int (converted_repo .stat ().st_mtime ))
6060
6161 def testUpdateForcesUpdates (self ):
6262 r = self .Pkgfile (['-u' ])
@@ -65,17 +65,15 @@ def testUpdateForcesUpdates(self):
6565 inodes_before = {}
6666 for r in ('multilib' , 'testing' ):
6767 for repofile in self .getRepoFiles (self .cachedir , r ):
68- inodes_before [os .path .basename (repofile )] = os .stat (
69- repofile ).st_ino
68+ inodes_before [repofile .name ] = repofile .stat ().st_ino
7069
7170 r = self .Pkgfile (['-uu' ])
7271 self .assertEqual (r .returncode , 0 )
7372
7473 inodes_after = {}
7574 for r in ('multilib' , 'testing' ):
7675 for repofile in self .getRepoFiles (self .cachedir , r ):
77- inodes_after [os .path .basename (repofile )] = os .stat (
78- repofile ).st_ino
76+ inodes_after [repofile .name ] = repofile .stat ().st_ino
7977
8078 for r in ('multilib' , 'testing' ):
8179 self .assertNotEqual (
@@ -87,25 +85,25 @@ def testUpdateSkipsUpToDate(self):
8785 r = self .Pkgfile (['-u' ])
8886 self .assertEqual (r .returncode , 0 )
8987
90- # gather mtimes
88+ # gather inodes before the update
9189 inodes_before = defaultdict (dict )
9290 for r in ('multilib' , 'testing' ):
9391 for repofile in self .getRepoFiles (self .cachedir , r ):
94- inodes_before [r ][repofile ] = os .stat (repofile ).st_ino
92+ inodes_before [r ][repofile ] = repofile .stat ().st_ino
9593
9694 # set the mtime to the epoch, expect that it gets rewritten on next update
97- os .utime (os . path . join ( self .cachedir , 'testing.files.000' ) , (0 , 0 ))
95+ os .utime (self .cachedir / 'testing.files.000' , (0 , 0 ))
9896
9997 r = self .Pkgfile (['-u' ])
10098 self .assertEqual (r .returncode , 0 )
10199
102- # re-gather mtimes after a soft update
100+ # re-gather inodes after a soft update
103101 inodes_after = defaultdict (dict )
104102 for r in ('multilib' , 'testing' ):
105103 for repofile in self .getRepoFiles (self .cachedir , r ):
106- inodes_after [r ][repofile ] = os .stat (repofile ).st_ino
104+ inodes_after [r ][repofile ] = repofile .stat ().st_ino
107105
108- # compare to mtimes after
106+ # compare inodes
109107 self .assertEqual (
110108 inodes_before ['multilib' ],
111109 inodes_after ['multilib' ],
@@ -117,53 +115,64 @@ def testUpdateSkipsUpToDate(self):
117115 msg = 'testing.files unexpectedly NOT rewritten by `pkgfile -u`' )
118116
119117 def testUpdateSkipsBadServer (self ):
120- with open (os .path .join (self .tempdir , 'pacman.conf' ), 'w' ) as f :
121- f .write ('''
118+ Path (self .tempdir / 'pacman.conf' ).write_text (f'''
122119 [options]
123120 Architecture = x86_64
124121
125122 [testing]
126- Server = {fakehttp_server }/$arch/$repo/404
127- Server = {fakehttp_server }/$arch/$repo
123+ Server = { self . baseurl } /$arch/$repo/404
124+ Server = { self . baseurl } /$arch/$repo
128125
129126 [multilib]
130- Server = {fakehttp_server }/$arch/$repo
131- ''' . format ( fakehttp_server = self . baseurl ) )
127+ Server = { self . baseurl } /$arch/$repo
128+ ''' )
132129
133130 r = self .Pkgfile (['-u' ])
134131 self .assertEqual (r .returncode , 0 )
135132
136133 def testUpdateFailsWhenExhaustingServers (self ):
137- with open (os .path .join (self .tempdir , 'pacman.conf' ), 'w' ) as f :
138- f .write ('''
134+ Path (self .tempdir , 'pacman.conf' ).write_text (f'''
139135 [options]
140136 Architecture = x86_64
141137
142138 [testing]
143- Server = {fakehttp_server }/$arch/$repo/404
139+ Server = { self . baseurl } /$arch/$repo/404
144140
145141 [multilib]
146- Server = {fakehttp_server }/$arch/$repo
147- ''' . format ( fakehttp_server = self . baseurl ) )
142+ Server = { self . baseurl } /$arch/$repo
143+ ''' )
148144
149145 r = self .Pkgfile (['-u' ])
150146 self .assertNotEqual (r .returncode , 0 )
151147
152148 def testUpdateCleansUpOldRepoChunks (self ):
153149 r = self .Pkgfile (['-uu' , '--repochunkbytes=5000' ])
154150 self .assertEqual (r .returncode , 0 )
155- small_chunks = set (glob .glob ('{}/testing.files*' .format (
156- self .cachedir )))
151+ small_chunks = set (Path (self .cachedir ).glob ('testing.files*' ))
157152
158153 # update again, creating ~half as many repo files
159154 r = self .Pkgfile (['-uu' , '--repochunkbytes=200000' ])
160155 self .assertEqual (r .returncode , 0 )
161- large_chunks = set (glob .glob ('{}/testing.files*' .format (
162- self .cachedir )))
156+ large_chunks = set (Path (self .cachedir ).glob ('testing.files*' ))
163157
164158 # the 100k chunked fileset is a strict subset of the original 5k chunked fileset.
165159 self .assertLess (large_chunks , small_chunks )
166160
161+ def testUpdateRemovesUnknownRepos (self ):
162+ expected_removed = (
163+ self .cachedir / "garbage.files" ,
164+ self .cachedir / "deletemebro.files.000" ,
165+ )
166+
167+ for p in expected_removed :
168+ p .touch ()
169+
170+ r = self .Pkgfile (['-u' ])
171+ self .assertEqual (r .returncode , 0 )
172+
173+ for p in expected_removed :
174+ self .assertFalse (p .exists (), msg = '{p} still exists, expected deleted' )
175+
167176
168177if __name__ == '__main__' :
169178 pkgfile_test .main ()
0 commit comments