From f836ca8d8b542d7e8d81ba100208d520782b526b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Virtus?= Date: Sat, 17 Jun 2023 23:24:01 +0200 Subject: [PATCH] testutil/pkgdata: Add TarEntry shorthand constructors Introduce shorthand constructors for testutil.TarEntry structures. These are Reg(), Dir() and Lnk() functions. The rationale for this addition is to make the test case definition less verbose. There are other changes in the queue that construct custom packages to test various test cases. In all these new tests (and old ones as well) we only care about file's type, path, mode and content. With these shorthand constructors and function aliases we can define tar entries like: Dir(0755, "./data/"), Reg(0600, "./data/document.txt", "words words words"), Lnk(0777, "./data/document", "document.txt"), Instead of: testutil.TarEntry{ Header: tar.Header{ Name: "./data/", Mode: 0755, }, }, testutil.TarEntry{ Header: tar.Header{ Name: "./document.txt", Mode: 0600, }, Content: []byte("words words words"), }, testutil.TarEntry{ Header: tar.Header{ Name: "./document.txt", Mode: 0777, Linkname: "document.txt", }, }, The reason for the 3 letter names and the order of arguments is to make the list of paths aligned on the same column in tarball definitions. These function only create barebone TarEntry. It'll still get adjusted when passed through fixupTarEntry(). --- internal/testutil/pkgdata.go | 38 ++++++++++++++++++++++++++++++ internal/testutil/pkgdata_test.go | 39 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/internal/testutil/pkgdata.go b/internal/testutil/pkgdata.go index 682b1ea9..7610cd11 100644 --- a/internal/testutil/pkgdata.go +++ b/internal/testutil/pkgdata.go @@ -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, + }, + } +} diff --git a/internal/testutil/pkgdata_test.go b/internal/testutil/pkgdata_test.go index 30136c3d..fb38b24a 100644 --- a/internal/testutil/pkgdata_test.go +++ b/internal/testutil/pkgdata_test.go @@ -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) + } +}