<commit_before>package tarx

import (
	"io/ioutil"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCompressFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 1, len(headers))
	assert.Equal(t, "a.txt", headers[0].Name)
}

func TestCompressFolder(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 6, len(headers))
	assert.Equal(t, "a.txt", headers[0].Name)
	assert.Equal(t, "b.txt", headers[1].Name)
	assert.Equal(t, "c", headers[2].Name)
	assert.Equal(t, "c/c1.txt", headers[3].Name)
	assert.Equal(t, "c/c2.txt", headers[4].Name)
	assert.Equal(t, "symlink.txt", headers[5].Name)
}

func TestCompressFolderWithIncludeSourceDir(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", &CompressOptions{IncludeSourceDir: true})
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 7, len(headers))
	assert.Equal(t, "input", headers[0].Name)
	assert.Equal(t, "input/a.txt", headers[1].Name)
	assert.Equal(t, "input/b.txt", headers[2].Name)
	assert.Equal(t, "input/c", headers[3].Name)
	assert.Equal(t, "input/c/c1.txt", headers[4].Name)
	assert.Equal(t, "input/c/c2.txt", headers[5].Name)
	assert.Equal(t, "input/symlink.txt", headers[6].Name)
}

func TestAppendFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/c", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	err = Compress(filename, "tests/input/a.txt", &CompressOptions{Append: true})
	assert.NoError(t, err)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 3, len(headers))
	assert.Equal(t, "c1.txt", headers[0].Name)
	assert.Equal(t, "c2.txt", headers[1].Name)
	assert.Equal(t, "a.txt", headers[2].Name)
}

func TestAppendFileWithGzip(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/c", &CompressOptions{Compression: Gzip})
	assert.NoError(t, err)
	defer os.Remove(filename)

	err = Compress(filename, "tests/input/a.txt", &CompressOptions{Append: true})
	assert.EqualError(t, ErrAppendNotSupported, err.Error())
}

func TestReadFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	header, reader, err := Read(filename, "a.txt")
	assert.Equal(t, nil, err)
	assert.Equal(t, "a.txt", header.Name)
	b, _ := ioutil.ReadAll(reader)
	assert.Equal(t, "a.txt\n", string(b))
	assert.Equal(t, nil, reader.Close())
}

func TestReadDir(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	_, reader, err := Read(filename, "c")
	assert.Equal(t, nil, reader)
	assert.Equal(t, nil, err)
}

func TestReadNotExists(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	_, _, err = Read(filename, "notExists.txt")
	assert.Equal(t, os.ErrNotExist, err)
}

func TestExtract(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)<commit_msg>Rename method Read to Find<commit_after>package tarx

import (
	"io/ioutil"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCompressFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 1, len(headers))
	assert.Equal(t, "a.txt", headers[0].Name)
}

func TestCompressFolder(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 6, len(headers))
	assert.Equal(t, "a.txt", headers[0].Name)
	assert.Equal(t, "b.txt", headers[1].Name)
	assert.Equal(t, "c", headers[2].Name)
	assert.Equal(t, "c/c1.txt", headers[3].Name)
	assert.Equal(t, "c/c2.txt", headers[4].Name)
	assert.Equal(t, "symlink.txt", headers[5].Name)
}

func TestCompressFolderWithIncludeSourceDir(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", &CompressOptions{IncludeSourceDir: true})
	assert.NoError(t, err)
	defer os.Remove(filename)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 7, len(headers))
	assert.Equal(t, "input", headers[0].Name)
	assert.Equal(t, "input/a.txt", headers[1].Name)
	assert.Equal(t, "input/b.txt", headers[2].Name)
	assert.Equal(t, "input/c", headers[3].Name)
	assert.Equal(t, "input/c/c1.txt", headers[4].Name)
	assert.Equal(t, "input/c/c2.txt", headers[5].Name)
	assert.Equal(t, "input/symlink.txt", headers[6].Name)
}

func TestAppendFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/c", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	err = Compress(filename, "tests/input/a.txt", &CompressOptions{Append: true})
	assert.NoError(t, err)

	headers, err := List(filename)
	assert.NoError(t, err)

	assert.Equal(t, 3, len(headers))
	assert.Equal(t, "c1.txt", headers[0].Name)
	assert.Equal(t, "c2.txt", headers[1].Name)
	assert.Equal(t, "a.txt", headers[2].Name)
}

func TestAppendFileWithGzip(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/c", &CompressOptions{Compression: Gzip})
	assert.NoError(t, err)
	defer os.Remove(filename)

	err = Compress(filename, "tests/input/a.txt", &CompressOptions{Append: true})
	assert.EqualError(t, ErrAppendNotSupported, err.Error())
}

func TestFindFile(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	header, reader, err := Find(filename, "a.txt")
	assert.Equal(t, nil, err)
	assert.Equal(t, "a.txt", header.Name)
	b, _ := ioutil.ReadAll(reader)
	assert.Equal(t, "a.txt\n", string(b))
	assert.Equal(t, nil, reader.Close())
}

func TestFindDir(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	_, reader, err := Find(filename, "c")
	assert.Equal(t, nil, reader)
	assert.Equal(t, nil, err)
}

func TestReadNotExists(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input/a.txt", nil)
	assert.NoError(t, err)
	defer os.Remove(filename)

	_, _, err = Find(filename, "notExists.txt")
	assert.Equal(t, os.ErrNotExist, err)
}

func TestExtract(t *testing.T) {
	filename := "tests/test.tar"

	err := Compress(filename, "tests/input", nil)
{'fraction_non_alphanumeric': 0.18777603647243196, 'fraction_numerical': 0.00769340361874911, 'mean_word_length': 5.067415730337078, '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': '7167789', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2754, 'n_words': 513}