Create mock os.Fileinfo

Create a function `sizer` (Probably ought to be Sizer or a bettername)
that allows one to specify what Size() returns. Used in testing.

Signed-off-by: Icarus Sparry <icarus.w.sparry@intel.com>
This commit is contained in:
Icarus Sparry
2018-02-06 10:25:08 -08:00
committed by Matthew Johnson
parent 70bc5ae9aa
commit 89dbdbc28b
2 changed files with 32 additions and 10 deletions
+10 -10
View File
@@ -369,21 +369,21 @@ func TestSortFilesVersionName(t *testing.T) {
func TestLinkPeersAndChange(t *testing.T) {
mOld := Manifest{
Files: []*File{
{Name: "1", Status: StatusUnset},
{Name: "2", Status: StatusDeleted},
{Name: "3", Status: StatusGhosted},
{Name: "4", Status: StatusUnset},
{Name: "5", Status: StatusUnset, Hash: 1},
{Name: "1", Status: StatusUnset, Info: sizer(0)},
{Name: "2", Status: StatusDeleted, Info: sizer(0)},
{Name: "3", Status: StatusGhosted, Info: sizer(0)},
{Name: "4", Status: StatusUnset, Info: sizer(0)},
{Name: "5", Status: StatusUnset, Hash: 1, Info: sizer(0)},
},
}
mNew := Manifest{
Files: []*File{
{Name: "1", Status: StatusUnset, Hash: 1},
{Name: "2", Status: StatusUnset},
{Name: "3", Status: StatusUnset},
{Name: "5", Status: StatusUnset, Hash: 2},
{Name: "6", Status: StatusUnset},
{Name: "1", Status: StatusUnset, Hash: 1, Info: sizer(0)},
{Name: "2", Status: StatusUnset, Info: sizer(0)},
{Name: "3", Status: StatusUnset, Info: sizer(0)},
{Name: "5", Status: StatusUnset, Hash: 2, Info: sizer(0)},
{Name: "6", Status: StatusUnset, Info: sizer(0)},
},
}
+22
View File
@@ -0,0 +1,22 @@
package swupd
import (
"os"
)
// Need to set up a type to hold the FileInfo
// as we are not holding them on disk
type mockinfo struct {
os.FileInfo // Embedded but only used to get things like Name() method
size int64
}
// Here is the method to override the FileInfo.Size() for mockinfo
func (m mockinfo) Size() int64 {
return m.size
}
// Constructor for os.FileInfo using mockinfo
func sizer(s int64) os.FileInfo {
return mockinfo{size: s}
}