<commit_before>package fix

import (
	"fmt"
	"github.com/ghthor/gospec"
	. "github.com/ghthor/gospec"
	"github.com/ghthor/journal/git"
	"github.com/ghthor/journal/git/gittest"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"
)

//A layout to use as the entry's filename
const filenameLayout = "2006-01-02-1504-MST"

type entryFilenames []string

func (f entryFilenames) Len() int { return len(f) }
func (f entryFilenames) Less(i, j int) bool {
	iTime, err := time.Parse(filenameLayout, f[i])
	if err != nil {
		panic(err)
	}

	jTime, err := time.Parse(filenameLayout, f[j])
	if err != nil {
		panic(err)
	}

	return jTime.After(iTime)
}
func (f entryFilenames) Swap(i, j int) { f[i], f[j] = f[j], f[i] }

func DescribeJournalCase0(c gospec.Context) {
	// Copy the journal_cases/case_0/ files to directory
	copyCase0Files := func(to string) (filenames []string) {
		err := filepath.Walk("journal_cases/case_0", func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if !info.IsDir() {
				fromFile, err := os.OpenFile(path, os.O_RDONLY, 0600)
				if err != nil {
					return err
				}
				defer fromFile.Close()

				toFile, err := os.OpenFile(filepath.Join(to, info.Name()), os.O_CREATE|os.O_WRONLY, info.Mode().Perm())
				if err != nil {
					return err
				}
				defer toFile.Close()

				_, err = io.Copy(toFile, fromFile)
				if err != nil {
					return err
				}

				filenames = append(filenames, info.Name())
			}
			return nil
		})

		c.Assume(err, IsNil)

		return
	}

	newCase0 := func(prefix string) (string, []string) {
		// Create a _test/ directory for case_0/
		d, err := ioutil.TempDir("_test", prefix+"_")
		c.Assume(err, IsNil)

		// git init
		c.Assume(git.Init(d), IsNil)

		// Copy case_0/ files
		entries := copyCase0Files(d)
		sort.Sort(entryFilenames(entries))

		// Commit all the entries
		for i, entryFilename := range entries {
			changes := git.NewChangesIn(d)
			changes.Add(git.ChangedFile(entryFilename))
			changes.Msg = fmt.Sprintf("Commit Msg | Entry %d\n", i+1)
			c.Assume(changes.Commit(), IsNil)
		}

		return d, entries
	}

	c.Specify("case 0", func() {
		c.Specify("is created as a git repository", func() {
			d, entries := newCase0("case_0_is_git")

			c.Expect(d, gittest.IsAGitRepository)
			c.Expect(git.IsClean(d), IsNil)

			c.Specify("and contains committed entry", func() {
				for i := 0; i < len(entries); i++ {
					entryFilename := entries[i]

					c.Specify(entryFilename, func() {
						// Check that the files were commited in the correct order
						o, err := git.Command(d, "show", "--name-only", "--pretty=format:",
							fmt.Sprintf("HEAD%s", strings.Repeat("^", len(entries)-1-i))).Output()
						c.Assume(err, IsNil)<commit_msg>Extracted setup functions from closures to functions<commit_after>package fix

import (
	"fmt"
	"github.com/ghthor/gospec"
	. "github.com/ghthor/gospec"
	"github.com/ghthor/journal/git"
	"github.com/ghthor/journal/git/gittest"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"
)

//A layout to use as the entry's filename
const filenameLayout = "2006-01-02-1504-MST"

type entryFilenames []string

func (f entryFilenames) Len() int { return len(f) }
func (f entryFilenames) Less(i, j int) bool {
	iTime, err := time.Parse(filenameLayout, f[i])
	if err != nil {
		panic(err)
	}

	jTime, err := time.Parse(filenameLayout, f[j])
	if err != nil {
		panic(err)
	}

	return jTime.After(iTime)
}
func (f entryFilenames) Swap(i, j int) { f[i], f[j] = f[j], f[i] }

// Copy the journal_cases/case_0/ files to directory
func copyCase0Files(to string) (filenames []string, err error) {
	err = filepath.Walk("journal_cases/case_0", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.IsDir() {
			fromFile, err := os.OpenFile(path, os.O_RDONLY, 0600)
			if err != nil {
				return err
			}
			defer fromFile.Close()

			toFile, err := os.OpenFile(filepath.Join(to, info.Name()), os.O_CREATE|os.O_WRONLY, info.Mode().Perm())
			if err != nil {
				return err
			}
			defer toFile.Close()

			_, err = io.Copy(toFile, fromFile)
			if err != nil {
				return err
			}

			filenames = append(filenames, info.Name())
		}
		return nil
	})

	return
}

func newCase0(prefix string) (string, []string, error) {
	// Create a _test/ directory for case_0/
	d, err := ioutil.TempDir("_test", prefix+"_")
	if err != nil {
		return d, nil, err
	}

	// git init
	err = git.Init(d)
	if err != nil {
		return d, nil, err
	}

	// Copy case_0/ files
	entries, err := copyCase0Files(d)
	if err != nil {
		return d, nil, err
	}

	sort.Sort(entryFilenames(entries))

	// Commit all the entries
	for i, entryFilename := range entries {
		changes := git.NewChangesIn(d)
		changes.Add(git.ChangedFile(entryFilename))
		changes.Msg = fmt.Sprintf("Commit Msg | Entry %d\n", i+1)
		err = changes.Commit()
		if err != nil {
			return d, entries, err
		}
	}

	return d, entries, nil
}

func DescribeJournalCase0(c gospec.Context) {
	c.Specify("case 0", func() {
		c.Specify("is created as a git repository", func() {
			d, entries, err := newCase0("case_0_is_git")
			c.Assume(err, IsNil)

			c.Expect(d, gittest.IsAGitRepository)
			c.Expect(git.IsClean(d), IsNil)

			c.Specify("and contains committed entry", func() {
				for i := 0; i < len(entries); i++ {
					entryFilename := entries[i]

					c.Specify(entryFilename, func() {
						// Check that the files were commited in the correct order
						o, err := git.Command(d, "show", "--name-only", "--pretty=format:",
							fmt.Sprintf("HEAD%s", strings.Repeat("^", len(entries)-1-i))).Output()
						c.Assume(err, IsNil)
{'fraction_non_alphanumeric': 0.15795053003533568, 'fraction_numerical': 0.01060070671378092, 'mean_word_length': 3.3346094946401226, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6299288', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2127, 'n_words': 527}