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

feat: add t0122 - tar #28

Merged
merged 12 commits into from
Apr 5, 2023
Prev Previous commit
Next Next commit
fix: nicer tar check
laurentsenta committed Apr 4, 2023
commit 92a45a9ad704f84f77712fa559025327e9b0f673
52 changes: 26 additions & 26 deletions tooling/check/tar.go
Original file line number Diff line number Diff line change
@@ -37,9 +37,16 @@ func (c *CheckIsTarFile) Check(v []byte) CheckOutput {
r := bytes.NewReader(v)
tr := tar.NewReader(r)

foundFiles := make(map[string]bool)
foundFilesWithContent := make(map[string]bool)
fileContents := make(map[string]string)
searchedFiles := make(map[string]bool)
filenames := make([]string, 0, len(c.fileNames))

for _, fileName := range c.fileNames {
searchedFiles[fileName] = false
}

for fileName := range c.filesWithContent {
searchedFiles[fileName] = false
}

for {
hdr, err := tr.Next()
@@ -61,39 +68,32 @@ func (c *CheckIsTarFile) Check(v []byte) CheckOutput {
Reason: fmt.Sprintf("failed to read file '%s' content: %v", hdr.Name, err),
}
}
fileContents[hdr.Name] = buf.String()

for _, fileName := range c.fileNames {
if hdr.Name == fileName {
foundFiles[fileName] = true
}
}
filenames = append(filenames, hdr.Name)

if content, ok := c.filesWithContent[hdr.Name]; ok {
if buf.String() == content {
foundFilesWithContent[hdr.Name] = true
}
if _, ok := searchedFiles[hdr.Name]; ok {
searchedFiles[hdr.Name] = true
}
}

for _, fileName := range c.fileNames {
if !foundFiles[fileName] {
var fileList strings.Builder
for name := range fileContents {
fileList.WriteString(fmt.Sprintf("'%s', ", name))
}
return CheckOutput{
Success: false,
Reason: fmt.Sprintf("file '%s' not found in tar archive. Found files: [%s]", fileName, fileList.String()),
if _, ok := c.filesWithContent[hdr.Name]; ok {
content := buf.String()

if content != c.filesWithContent[hdr.Name] {
return CheckOutput{
Success: false,
Reason: fmt.Sprintf("file '%s' with expected content '%s' not found in tar archive. Actual content: '%s'", hdr.Name, c.filesWithContent[hdr.Name], content),
}
}
}
}

for fileName, content := range c.filesWithContent {
if !foundFilesWithContent[fileName] {
for name, found := range searchedFiles {
if !found {
allFiles := strings.Join(filenames, ", ")

return CheckOutput{
Success: false,
Reason: fmt.Sprintf("file '%s' with expected content '%s' not found in tar archive. Actual content: '%s'", fileName, content, fileContents[fileName]),
Reason: fmt.Sprintf("file '%s' not found in tar archive. Found files: [%s]", name, allFiles),
}
}
}