<commit_before>package targz

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"testing"
)

func Test_CompressAndExtract(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	structureBefore := directoryStructureString(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
	if err != nil {
		t.Errorf("Extract error: %s", err)
	}

	structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

	if structureAfter != structureBefore {
		t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)
	}
}

func Test_GivesErrorIfOutputIsFile(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that my_archive.tar.gz isn't a directory")
	}
}

func Test_GivesErrorIfInputDirDoesNotExist(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	os.RemoveAll(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that %s doesn't exist", dirToCompress)
	}
}

func Test_GivesErrorIfInputDirIsEmpty(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	os.RemoveAll(filepath.Join(dirToCompress, "my_sub_folder"))

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that %s is empty", dirToCompress)
	}
}

func Test_CompressAndExtractWithMultipleFiles(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	createFiles(dirToCompress, "file1.txt", "file2.txt", "file3.txt")

	structureBefore := directoryStructureString(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
	if err != nil {
		t.Errorf("Extract error: %s", err)
	}

	structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

	if structureAfter != structureBefore {
		t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)
	}
}

func Test_CompabilityWithTar(t *testing.T) {
	_, err := exec.LookPath("tar")
	if err == nil {
		tmpDir, dirToCompress := createTestData()
		defer os.RemoveAll(tmpDir)

		structureBefore := directoryStructureString(dirToCompress)

		err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
		if err != nil {
			t.Errorf("Comress error: %s", err)
		}

		os.MkdirAll(filepath.Join(tmpDir, "extracted"), 0755)
		cmd := exec.Command("tar", "xfz", filepath.Join(tmpDir, "my_archive.tar.gz"), "-C", filepath.Join(tmpDir, "extracted"))
		err = cmd.Run()
		if err != nil {
			fmt.Println("Run error")
			panic(err)
		}

		structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

		if structureAfter != structureBefore {
			t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)<commit_msg>Add test for removing output directory on compress fail<commit_after>package targz

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"testing"
)

func Test_CompressAndExtract(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	structureBefore := directoryStructureString(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
	if err != nil {
		t.Errorf("Extract error: %s", err)
	}

	structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

	if structureAfter != structureBefore {
		t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)
	}
}

func Test_GivesErrorIfOutputIsFile(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that my_archive.tar.gz isn't a directory")
	}
}

func Test_GivesErrorIfInputDirDoesNotExist(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	os.RemoveAll(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that %s doesn't exist", dirToCompress)
	}
}

func Test_GivesErrorIfInputDirIsEmpty(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	os.RemoveAll(filepath.Join(dirToCompress, "my_sub_folder"))

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that %s is empty", dirToCompress)
	}
}

func Test_CompressAndExtractWithMultipleFiles(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	createFiles(dirToCompress, "file1.txt", "file2.txt", "file3.txt")

	structureBefore := directoryStructureString(dirToCompress)

	err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
	if err != nil {
		t.Errorf("Comress error: %s", err)
	}

	err = Extract(filepath.Join(tmpDir, "my_archive.tar.gz"), filepath.Join(tmpDir, "extracted"))
	if err != nil {
		t.Errorf("Extract error: %s", err)
	}

	structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

	if structureAfter != structureBefore {
		t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)
	}
}

func Test_ThatOutputDirIsRemovedIfCompressFails(t *testing.T) {
	tmpDir, dirToCompress := createTestData()
	defer os.RemoveAll(tmpDir)

	os.RemoveAll(filepath.Join(dirToCompress, "my_sub_folder"))

	err := Compress(dirToCompress, filepath.Join(tmpDir, "dir_to_be_removed", "my_archive.tar.gz"))
	if err == nil {
		t.Errorf("Should say that %s is empty", dirToCompress)
	}

	exist, err := exists(filepath.Join(tmpDir, "dir_to_be_removed"))
	if err != nil {
		panic(err)
	}
	if exist {
		t.Errorf("%s should be removed", filepath.Join(tmpDir, "dir_to_be_removed"))
	}
}

func Test_CompabilityWithTar(t *testing.T) {
	_, err := exec.LookPath("tar")
	if err == nil {
		tmpDir, dirToCompress := createTestData()
		defer os.RemoveAll(tmpDir)

		structureBefore := directoryStructureString(dirToCompress)

		err := Compress(dirToCompress, filepath.Join(tmpDir, "my_archive.tar.gz"))
		if err != nil {
			t.Errorf("Comress error: %s", err)
		}

		os.MkdirAll(filepath.Join(tmpDir, "extracted"), 0755)
		cmd := exec.Command("tar", "xfz", filepath.Join(tmpDir, "my_archive.tar.gz"), "-C", filepath.Join(tmpDir, "extracted"))
		err = cmd.Run()
		if err != nil {
			fmt.Println("Run error")
			panic(err)
		}

		structureAfter := directoryStructureString(filepath.Join(tmpDir, "extracted", "my_folder"))

		if structureAfter != structureBefore {
			t.Errorf("Directory structure before compress and after extract does not match. Before {%s}, After {%s}", structureBefore, structureAfter)
{'fraction_non_alphanumeric': 0.145183343691734, 'fraction_numerical': 0.001740211311373524, 'mean_word_length': 5.462650602409639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2793812', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2819, 'n_words': 596}