Unverified Commit 0d343195 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Add tests for archiving/extracting .git directory

parent 84b31908
package archives
import (
"fmt"
"path/filepath"
"strings"
......@@ -18,16 +19,11 @@ func doesPathsListContainGitDirectory(paths []string) bool {
return false
}
func warnIfTryingToArchiveGitDirectory(paths []string) {
if doesPathsListContainGitDirectory(paths) {
logrus.Warn("Part of .git directory is on the list of files to archive")
logrus.Warn("This may introduce unexpected problems")
func warnOnGitDirectory(operation string, paths []string) {
if !doesPathsListContainGitDirectory(paths) {
return
}
}
func warnIfTryingToExtractGitDirectory(paths []string) {
if doesPathsListContainGitDirectory(paths) {
logrus.Warn("Part of .git directory is on the list of files to extract")
logrus.Warn("This may introduce unexpected problems")
}
logrus.Warn(fmt.Sprintf("Part of .git directory is on the list of files to %s", operation))
logrus.Warn("This may introduce unexpected problems")
}
......@@ -83,7 +83,7 @@ func createZipEntry(archive *zip.Writer, fileName string) error {
}
func CreateZipArchive(w io.Writer, fileNames []string) error {
warnIfTryingToArchiveGitDirectory(fileNames)
warnOnGitDirectory("archive", fileNames)
archive := zip.NewWriter(w)
defer archive.Close()
......
......@@ -2,12 +2,15 @@ package archives
import (
"archive/zip"
"bytes"
"io/ioutil"
"os"
"syscall"
"testing"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testZipFileContent = []byte("test content")
......@@ -36,49 +39,88 @@ func createTestPipe(t *testing.T) string {
return "test_pipe"
}
func TestZipCreate(t *testing.T) {
td, err := ioutil.TempDir("", "zip_create")
if !assert.NoError(t, err) {
return
}
func createTestGitPathFile(t *testing.T) string {
err := os.Mkdir(".git", 0711)
assert.NoError(t, err)
err = ioutil.WriteFile(".git/test_file", testZipFileContent, 0640)
assert.NoError(t, err)
return ".git/test_file"
}
func testInWorkDir(t *testing.T, testCase func(t *testing.T, fileName string)) {
wd, err := os.Getwd()
assert.NoError(t, err)
defer os.Chdir(wd)
td, err := ioutil.TempDir("", "zip_create")
require.NoError(t, err)
err = os.Chdir(td)
assert.NoError(t, err)
tempFile, err := ioutil.TempFile("", "archive")
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
tempFile.Close()
defer os.Remove(tempFile.Name())
err = CreateZipFile(tempFile.Name(), []string{
createTestFile(t),
createSymlinkFile(t),
createTestDirectory(t),
createTestPipe(t),
"non_existing_file.txt",
testCase(t, tempFile.Name())
}
func TestZipCreate(t *testing.T) {
testInWorkDir(t, func(t *testing.T, fileName string) {
paths := []string{
createTestFile(t),
createSymlinkFile(t),
createTestDirectory(t),
createTestPipe(t),
"non_existing_file.txt",
}
err := CreateZipFile(fileName, paths)
require.NoError(t, err)
archive, err := zip.OpenReader(fileName)
require.NoError(t, err)
defer archive.Close()
assert.Len(t, archive.File, 3)
assert.Equal(t, "test_file.txt", archive.File[0].Name)
assert.Equal(t, os.FileMode(0640), archive.File[0].Mode().Perm())
assert.NotEmpty(t, archive.File[0].Extra)
assert.Equal(t, "new_symlink", archive.File[1].Name)
assert.Equal(t, "test_directory/", archive.File[2].Name)
assert.NotEmpty(t, archive.File[2].Extra)
assert.True(t, archive.File[2].Mode().IsDir())
})
}
func TestZipCreateWithGitPath(t *testing.T) {
testInWorkDir(t, func(t *testing.T, fileName string) {
output := logrus.StandardLogger().Out
var buf bytes.Buffer
logrus.SetOutput(&buf)
defer logrus.SetOutput(output)
paths := []string{
createTestGitPathFile(t),
}
err := CreateZipFile(fileName, paths)
require.NoError(t, err)
assert.Contains(t, buf.String(), "Part of .git directory is on the list of files to archive")
archive, err := zip.OpenReader(fileName)
require.NoError(t, err)
defer archive.Close()
assert.Len(t, archive.File, 1)
assert.Equal(t, ".git/test_file", archive.File[0].Name)
assert.Equal(t, os.FileMode(0640), archive.File[0].Mode().Perm())
assert.NotEmpty(t, archive.File[0].Extra)
})
if !assert.NoError(t, err) {
return
}
archive, err := zip.OpenReader(tempFile.Name())
if !assert.NoError(t, err) {
return
}
defer archive.Close()
assert.Len(t, archive.File, 3)
assert.Equal(t, "test_file.txt", archive.File[0].Name)
assert.Equal(t, os.FileMode(0640), archive.File[0].Mode().Perm())
assert.NotEmpty(t, archive.File[0].Extra)
assert.Equal(t, "new_symlink", archive.File[1].Name)
assert.Equal(t, "test_directory/", archive.File[2].Name)
assert.NotEmpty(t, archive.File[2].Extra)
assert.True(t, archive.File[2].Mode().IsDir())
}
......@@ -102,7 +102,7 @@ func ExtractZipArchive(archive *zip.Reader) error {
paths = append(paths, file.Name)
}
warnIfTryingToExtractGitDirectory(paths)
warnOnGitDirectory("extract", paths)
return nil
}
......
......@@ -2,50 +2,87 @@ package archives
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"os"
"testing"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func writeArchive(t *testing.T, w io.Writer) {
archive := zip.NewWriter(w)
defer archive.Close()
func createDefaultArchive(t *testing.T, archive *zip.Writer) {
testFile, err := archive.Create("temporary_file.txt")
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
io.WriteString(testFile, "test file")
}
func TestExtractZipFile(t *testing.T) {
func createArchiveWithGitPath(t *testing.T, archive *zip.Writer) {
testGitFile, err := archive.Create(".git/test_file")
require.NoError(t, err)
io.WriteString(testGitFile, "test git file")
}
func testOnArchive(t *testing.T, createArchive func(t *testing.T, archive *zip.Writer), testCase func(t *testing.T, fileName string)) {
tempFile, err := ioutil.TempFile("", "archive")
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
defer tempFile.Close()
defer os.Remove(tempFile.Name())
writeArchive(t, tempFile)
archive := zip.NewWriter(tempFile)
defer archive.Close()
createArchive(t, archive)
archive.Close()
tempFile.Close()
err = ExtractZipFile(tempFile.Name())
if !assert.NoError(t, err) {
return
}
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
testCase(t, tempFile.Name())
}
func TestExtractZipFile(t *testing.T) {
testOnArchive(t, createDefaultArchive, func(t *testing.T, fileName string) {
err := ExtractZipFile(fileName)
require.NoError(t, err)
stat, err := os.Stat("temporary_file.txt")
assert.False(t, os.IsNotExist(err), "Expected temporary_file.txt to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
if stat != nil {
defer os.Remove("temporary_file.txt")
assert.Equal(t, int64(9), stat.Size())
}
})
}
func TestExtractZipFileWithGitPath(t *testing.T) {
testOnArchive(t, createArchiveWithGitPath, func(t *testing.T, fileName string) {
output := logrus.StandardLogger().Out
var buf bytes.Buffer
logrus.SetOutput(&buf)
defer logrus.SetOutput(output)
err := ExtractZipFile(fileName)
require.NoError(t, err)
assert.Contains(t, buf.String(), "Part of .git directory is on the list of files to extract")
stat, err := os.Stat(".git/test_file")
assert.False(t, os.IsNotExist(err), "Expected .git/test_file to exist")
if !os.IsNotExist(err) {
assert.NoError(t, err)
}
if stat != nil {
defer os.Remove(".git/test_file")
assert.Equal(t, int64(13), stat.Size())
}
})
}
func TestExtractZipFileNotFound(t *testing.T) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment