MCPcopy Index your code
hub / github.com/coder/coder / TestCreateZipFromTar

Function TestCreateZipFromTar

archive/archive_test.go:146–208  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

144}
145
146func TestCreateZipFromTar(t *testing.T) {
147 t.Parallel()
148 if runtime.GOOS != "linux" {
149 t.Skip("skipping this test on non-Linux platform")
150 }
151 t.Run("OK", func(t *testing.T) {
152 t.Parallel()
153 tarBytes := archivetest.TestTarFileBytes()
154
155 tr := tar.NewReader(bytes.NewReader(tarBytes))
156 zipBytes, err := archive.CreateZipFromTar(tr, int64(len(tarBytes)))
157 require.NoError(t, err)
158
159 archivetest.AssertSampleZipFile(t, zipBytes)
160
161 tempDir := t.TempDir()
162 tempFilePath := filepath.Join(tempDir, "test.zip")
163 err = os.WriteFile(tempFilePath, zipBytes, 0o600)
164 require.NoError(t, err, "failed to write converted zip file")
165
166 ctx := testutil.Context(t, testutil.WaitShort)
167 cmd := exec.CommandContext(ctx, "unzip", tempFilePath, "-d", tempDir)
168 require.NoError(t, cmd.Run(), "failed to extract converted zip file")
169
170 assertExtractedFiles(t, tempDir, false)
171 })
172
173 t.Run("MissingSlashInDirectoryHeader", func(t *testing.T) {
174 t.Parallel()
175
176 // Given: a tar archive containing a directory entry that has the directory
177 // mode bit set but the name is missing a trailing slash
178
179 var tarBytes bytes.Buffer
180 tw := tar.NewWriter(&tarBytes)
181 tw.WriteHeader(&tar.Header{
182 Name: "dir",
183 Typeflag: tar.TypeDir,
184 Size: 0,
185 })
186 require.NoError(t, tw.Flush())
187 require.NoError(t, tw.Close())
188
189 // When: we convert this to a zip
190 tr := tar.NewReader(&tarBytes)
191 zipBytes, err := archive.CreateZipFromTar(tr, int64(tarBytes.Len()))
192 require.NoError(t, err)
193
194 // Then: the resulting zip should contain a corresponding directory
195 zr, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes)))
196 require.NoError(t, err)
197 for _, zf := range zr.File {
198 switch zf.Name {
199 case "dir":
200 require.Fail(t, "missing trailing slash in directory name")
201 case "dir/":
202 require.True(t, zf.Mode().IsDir(), "should be a directory")
203 default:

Callers

nothing calls this directly

Calls 15

TestTarFileBytesFunction · 0.92
CreateZipFromTarFunction · 0.92
AssertSampleZipFileFunction · 0.92
ContextFunction · 0.92
assertExtractedFilesFunction · 0.85
SkipMethod · 0.80
RunMethod · 0.65
TempDirMethod · 0.65
WriteFileMethod · 0.65
CommandContextMethod · 0.65
FlushMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected