Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testutil/pkgdata: Add TarEntry shorthand constructors #86

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/testutil/pkgdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,41 @@ func MustMakeDeb(entries []TarEntry) []byte {
}
return data
}

// Reg is a shortcut for creating a regular file TarEntry structure (with
// tar.Typeflag set tar.TypeReg). Reg stands for "REGular file".
func Reg(mode int64, path, content string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeReg,
Name: path,
Mode: mode,
},
Content: []byte(content),
}
}

// Dir is a shortcut for creating a directory TarEntry structure (with
// tar.Typeflag set to tar.TypeDir). Dir stands for "DIRectory".
func Dir(mode int64, path string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeDir,
Name: path,
Mode: mode,
},
}
}

// Lnk is a shortcut for creating a symbolic link TarEntry structure (with
// tar.Typeflag set to tar.TypeSymlink). Lnk stands for "symbolic LiNK".
func Lnk(mode int64, path, target string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeSymlink,
Name: path,
Mode: mode,
Linkname: target,
},
}
}
39 changes: 39 additions & 0 deletions internal/testutil/pkgdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,42 @@ func (s *S) TestMustMakeDeb(c *C) {
},
}})
}

func (s *S) TestTarEntryShortHands(c *C) {
var testCases = []struct {
shorthand testutil.TarEntry
result testutil.TarEntry
}{{
testutil.Reg(0600, "./document.txt", "cats are best"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeReg,
Name: "./document.txt",
Mode: 0600,
},
Content: []byte("cats are best"),
},
}, {
testutil.Dir(0755, "./home/user"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeDir,
Name: "./home/user",
Mode: 0755,
},
},
}, {
testutil.Lnk(0755, "./lib", "./usr/lib/"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeSymlink,
Name: "./lib",
Mode: 0755,
Linkname: "./usr/lib/",
},
},
}}
for _, test := range testCases {
c.Assert(test.shorthand, DeepEquals, test.result)
}
}