<commit_before>// Copyright 2015 <NAME>. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

package scm

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/maruel/pre-commit-go/internal"
	"github.com/maruel/ut"
)

func TestGetRepoGitSlow(t *testing.T) {
	// Make a repository and test behavior against it.
	tmpDir, err := ioutil.TempDir("", "pre-commit-go")
	defer func() {
		if err := os.RemoveAll(tmpDir); err != nil {
			t.Errorf("%s", err)
		}
	}()

	_, _, err = internal.CaptureWd(tmpDir, "git", "init")
	ut.AssertEqual(t, nil, err)
	repo, err := GetRepo(tmpDir)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, tmpDir, repo.Root())
	p, err := repo.PreCommitHookPath()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, filepath.Join(tmpDir, ".git", "hooks", "pre-commit"), p)
	ut.AssertEqual(t, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", repo.HEAD())

	untracked, err := repo.Untracked()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string(nil), untracked)

	unstaged, err := repo.Unstaged()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string(nil), unstaged)

	write(t, tmpDir, "file1", "hi\n")
	check(t, repo, []string{"file1"}, nil)

	run(t, tmpDir, "add", "file1")
	check(t, repo, nil, nil)

	done, err := repo.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, false, done)

	write(t, tmpDir, "file1", "hi\nhello\n")
	check(t, repo, nil, []string{"file1"})

	done, err = repo.Stash()
	ut.AssertEqual(t, errors.New("Can't stash until there's at least one commit"), err)
	ut.AssertEqual(t, false, done)

	run(t, tmpDir, "commit", "-m", "yo")
	ut.AssertEqual(t, "hi\nhello\n", read(t, tmpDir, "file1"))

	done, err = repo.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, true, done)
	ut.AssertEqual(t, "hi\n", read(t, tmpDir, "file1"))
	ut.AssertEqual(t, nil, repo.Restore())
	ut.AssertEqual(t, "hi\nhello\n", read(t, tmpDir, "file1"))
}

func check(t *testing.T, repo Repo, untracked []string, unstaged []string) {
	actualUntracked, err := repo.Untracked()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, untracked, actualUntracked)
	actualUnstaged, err := repo.Unstaged()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, unstaged, actualUnstaged)
}

func run(t *testing.T, tmpDir string, args ...string) {
	internal := &git{root: tmpDir}
	_, code, err := internal.capture(args...)
	ut.AssertEqual(t, 0, code)
	ut.AssertEqual(t, nil, err)
}

func write(t *testing.T, tmpDir, name string, content string) {
	ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(tmpDir, name), []byte(content), 0600))
}
<commit_msg>Fix test when user.email is not set, e.g. on travis.<commit_after>// Copyright 2015 <NAME>. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

package scm

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/maruel/pre-commit-go/internal"
	"github.com/maruel/ut"
)

func TestGetRepoGitSlow(t *testing.T) {
	// Make a repository and test behavior against it.
	tmpDir, err := ioutil.TempDir("", "pre-commit-go")
	defer func() {
		if err := os.RemoveAll(tmpDir); err != nil {
			t.Errorf("%s", err)
		}
	}()

	_, _, err = internal.CaptureWd(tmpDir, "git", "init")
	ut.AssertEqual(t, nil, err)
	run(t, tmpDir, "config", "user.email", "nobody@localhost")
	run(t, tmpDir, "config", "user.name", "nobody")
	repo, err := GetRepo(tmpDir)
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, tmpDir, repo.Root())
	p, err := repo.PreCommitHookPath()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, filepath.Join(tmpDir, ".git", "hooks", "pre-commit"), p)
	ut.AssertEqual(t, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", repo.HEAD())

	untracked, err := repo.Untracked()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string(nil), untracked)

	unstaged, err := repo.Unstaged()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, []string(nil), unstaged)

	write(t, tmpDir, "file1", "hi\n")
	check(t, repo, []string{"file1"}, nil)

	run(t, tmpDir, "add", "file1")
	check(t, repo, nil, nil)

	done, err := repo.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, false, done)

	write(t, tmpDir, "file1", "hi\nhello\n")
	check(t, repo, nil, []string{"file1"})

	done, err = repo.Stash()
	ut.AssertEqual(t, errors.New("Can't stash until there's at least one commit"), err)
	ut.AssertEqual(t, false, done)

	run(t, tmpDir, "commit", "-m", "yo")
	ut.AssertEqual(t, "hi\nhello\n", read(t, tmpDir, "file1"))

	done, err = repo.Stash()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, true, done)
	ut.AssertEqual(t, "hi\n", read(t, tmpDir, "file1"))
	ut.AssertEqual(t, nil, repo.Restore())
	ut.AssertEqual(t, "hi\nhello\n", read(t, tmpDir, "file1"))
}

func check(t *testing.T, repo Repo, untracked []string, unstaged []string) {
	actualUntracked, err := repo.Untracked()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, untracked, actualUntracked)
	actualUnstaged, err := repo.Unstaged()
	ut.AssertEqual(t, nil, err)
	ut.AssertEqual(t, unstaged, actualUnstaged)
}

func run(t *testing.T, tmpDir string, args ...string) {
	internal := &git{root: tmpDir}
	out, code, err := internal.capture(args...)
	ut.AssertEqualf(t, 0, code, "%s", out)
	ut.AssertEqual(t, nil, err)
}

func write(t *testing.T, tmpDir, name string, content string) {
	ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(tmpDir, name), []byte(content), 0600))
}

{'fraction_non_alphanumeric': 0.18442174388909158, 'fraction_numerical': 0.01568770521707406, 'mean_word_length': 4.747379454926625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2308308', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2203, 'n_words': 526}