<commit_before>package cmd

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

	"github.com/opalmer/gerrittest"
	. "gopkg.in/check.v1"
)

type IntegrationTest struct{}

var _ = Suite(&IntegrationTest{})

func (s *IntegrationTest) Test_StartStop(c *C) {
	if testing.Short() {
		c.Skip("-short set")
	}
	privateFile, err := ioutil.TempFile("", "")
	c.Assert(err, IsNil)
	defer os.Remove(privateFile.Name()) // nolint: errcheck

	// Generate a custom private key and pass it to Start(). This is
	// important to do because we want to replicate a case where a user
	// provides their own key and wants to cleanup after a test run. The
	// expectation is that gerrittest *does not* delete the key during
	// cleanup.
	private, err := gerrittest.GenerateRSAKey()
	c.Assert(err, IsNil)
	c.Assert(gerrittest.WriteRSAKey(private, privateFile), IsNil)

	file, err := ioutil.TempFile("", "")
	c.Assert(err, IsNil)
	defer os.Remove(file.Name()) // nolint: errcheck
	flags := []string{"--json", file.Name(), "--private-key", privateFile.Name()}

	// Call the start subcommand and write the struct to disk.
	c.Assert(file.Close(), IsNil)
	c.Assert(Start.ParseFlags(flags), IsNil)
	c.Assert(Start.RunE(Start, []string{}), IsNil)

	// Load information from json and then perform a few common
	// operations.
	gerrit, err := gerrittest.NewFromJSON(file.Name())
	c.Assert(err, IsNil)
	defer gerrit.Container.Terminate()   // nolint: errcheck
	defer os.RemoveAll(gerrit.Repo.Path) // nolint: errcheck

	client, err := gerrit.HTTP.Gerrit()
	c.Assert(err, IsNil)

	_, _, err = client.Projects.CreateProject("testing", nil)
	c.Assert(err, IsNil)

	c.Assert(gerrit.Repo.AddRemoteFromContainer(gerrit.Container, "", "testing"), IsNil)
	c.Assert(gerrit.Repo.AddContent("foo/bar.txt", 0600, []byte("Hello")), IsNil)
	c.Assert(gerrit.Repo.Commit("42: hello"), IsNil) // Note, the number is an attempt to mess up Push()
	c.Assert(gerrit.Repo.Push("", ""), IsNil)
	change, err := gerrit.Repo.ChangeID()
	c.Assert(err, IsNil)

	_, _, err = client.Changes.GetChangeDetail(change, nil)
	c.Assert(err, IsNil)

	// Read the struct from disk and use it to stop the container. This should
	// also cleanup the repository.
	c.Assert(Stop.ParseFlags([]string{"--json", file.Name()}), IsNil)
	c.Assert(Stop.RunE(Stop, []string{}), IsNil)

	// The below test to make sure that the custom private key and
	// repository were not removed when cleanup was run.
	_, err = os.Stat(privateFile.Name())
	c.Assert(err, IsNil)
	_, err = os.Stat(gerrit.Repo.Path)
	c.Assert(os.IsNotExist(err), Equals, true)
}<commit_msg>more comments<commit_after>package cmd

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

	"github.com/opalmer/gerrittest"
	. "gopkg.in/check.v1"
)

type IntegrationTest struct{}

var _ = Suite(&IntegrationTest{})

func (s *IntegrationTest) Test_StartStop(c *C) {
	if testing.Short() {
		c.Skip("-short set")
	}
	privateFile, err := ioutil.TempFile("", "")
	c.Assert(err, IsNil)
	defer os.Remove(privateFile.Name()) // nolint: errcheck

	// Generate a custom private key and pass it to Start(). This is
	// important to do because we want to replicate a case where a user
	// provides their own key and wants to cleanup after a test run. The
	// expectation is that gerrittest *does not* delete the key during
	// cleanup.
	private, err := gerrittest.GenerateRSAKey()
	c.Assert(err, IsNil)
	c.Assert(gerrittest.WriteRSAKey(private, privateFile), IsNil)

	file, err := ioutil.TempFile("", "")
	c.Assert(err, IsNil)
	defer os.Remove(file.Name()) // nolint: errcheck
	flags := []string{"--json", file.Name(), "--private-key", privateFile.Name()}

	// Call the start subcommand and write the struct to disk.
	c.Assert(file.Close(), IsNil)
	c.Assert(Start.ParseFlags(flags), IsNil)
	c.Assert(Start.RunE(Start, []string{}), IsNil)

	// Load information from json and then perform a few common
	// operations.
	gerrit, err := gerrittest.NewFromJSON(file.Name())
	c.Assert(err, IsNil)
	defer gerrit.Container.Terminate()   // nolint: errcheck
	defer os.RemoveAll(gerrit.Repo.Path) // nolint: errcheck

	client, err := gerrit.HTTP.Gerrit()
	c.Assert(err, IsNil)

	_, _, err = client.Projects.CreateProject("testing", nil)
	c.Assert(err, IsNil)

	// Add a remote based on the container then push a commit to it.
	c.Assert(gerrit.Repo.AddRemoteFromContainer(gerrit.Container, "", "testing"), IsNil)
	c.Assert(gerrit.Repo.AddContent("foo/bar.txt", 0600, []byte("Hello")), IsNil)
	c.Assert(gerrit.Repo.Commit("42: hello"), IsNil) // Note, the number is an attempt to mess up Push()
	c.Assert(gerrit.Repo.Push("", ""), IsNil)
	change, err := gerrit.Repo.ChangeID()
	c.Assert(err, IsNil)

	// Use the change id retrieved from git to retrieve information
	// about the change.
	_, _, err = client.Changes.GetChangeDetail(change, nil)
	c.Assert(err, IsNil)

	// Read the struct from disk and use it to stop the container. This should
	// also cleanup the repository.
	c.Assert(Stop.ParseFlags([]string{"--json", file.Name()}), IsNil)
	c.Assert(Stop.RunE(Stop, []string{}), IsNil)

	// The below test to make sure that the custom private key and
	// repository were not removed when cleanup was run.
	_, err = os.Stat(privateFile.Name())
	c.Assert(err, IsNil)
	_, err = os.Stat(gerrit.Repo.Path)
	c.Assert(os.IsNotExist(err), Equals, true)
}
{'fraction_non_alphanumeric': 0.15323345344206335, 'fraction_numerical': 0.0026550350843921864, 'mean_word_length': 4.211462450592886, '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': '3728072', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1831, 'n_words': 503}