<commit_before>package testutil

import (
	"bytes"
	"errors"
	"io/ioutil"
	"regexp"
	"testing"

	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app"
	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/config"
	"github.com/BytemarkHosting/bytemark-client/mocks"
	"github.com/BytemarkHosting/bytemark-client/util/log"
	"github.com/urfave/cli"
)

// GetBuf returns the captured output buffer for the given app.
// Use this in your tests to find out if your command's output is correct.
// TODO: add an example
func GetBuf(app *cli.App) (buf *bytes.Buffer, err error) {
	if bufInterface, ok := app.Metadata["buf"]; ok {
		if buf, ok = bufInterface.(*bytes.Buffer); ok {
			return
		}
		err = errors.New("couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
		return
	}
	err = errors.New("couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
	return
}

// AssertOutput fails the test unless the app under test has output exactly the
// expected string.
func AssertOutput(t *testing.T, app *cli.App, expected string) {
	buf, err := GetBuf(app)

	if err == nil {
		actual := buf.String()
		if actual != expected {
			t.Errorf("expected %q, got %q", expected, actual)
		}
	} else {
		t.Error("Couldn't recover the buffer - use BaseTestSetup to create your cli.Apps!")
	}
}

// AssertOutputMatch fails the test unless the app under test has produced
// output which matches the supplied regex.
func AssertOutputMatch(t *testing.T, app *cli.App, expected regexp.Regexp) {
	buf, err := GetBuf(app)

	if err != nil {
		t.Errorf("Couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
		return
	}
	if !expected.Match(buf.Bytes()) {
		return
	}
}

// BaseTestSetup constructs mock config and client and produces a cli.App with the given commands.
func BaseTestSetup(t *testing.T, admin bool, commands []cli.Command) (conf *mocks.Config, client *mocks.Client, cliapp *cli.App) {
	cli.OsExiter = func(_ int) {}
	conf = new(mocks.Config)
	client = new(mocks.Client)
	conf.When("GetBool", "admin").Return(admin, nil)
	conf.When("GetV", "output-format").Return(config.Var{"output-format", "human", "CODE"})

	cliapp, err := app.BaseAppSetup(app.GlobalFlags(), commands)<commit_msg>fix AssertOutputMatch<commit_after>package testutil

import (
	"bytes"
	"errors"
	"io/ioutil"
	"regexp"
	"testing"

	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app"
	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/config"
	"github.com/BytemarkHosting/bytemark-client/mocks"
	"github.com/BytemarkHosting/bytemark-client/util/log"
	"github.com/urfave/cli"
)

// GetBuf returns the captured output buffer for the given app.
// Use this in your tests to find out if your command's output is correct.
// TODO: add an example
func GetBuf(app *cli.App) (buf *bytes.Buffer, err error) {
	if bufInterface, ok := app.Metadata["buf"]; ok {
		if buf, ok = bufInterface.(*bytes.Buffer); ok {
			return
		}
		err = errors.New("couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
		return
	}
	err = errors.New("couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
	return
}

// AssertOutput fails the test unless the app under test has output exactly the
// expected string.
func AssertOutput(t *testing.T, app *cli.App, expected string) {
	buf, err := GetBuf(app)

	if err == nil {
		actual := buf.String()
		if actual != expected {
			t.Errorf("expected %q, got %q", expected, actual)
		}
	} else {
		t.Error("Couldn't recover the buffer - use BaseTestSetup to create your cli.Apps!")
	}
}

// AssertOutputMatch fails the test unless the app under test has produced
// output which matches the supplied regex.
func AssertOutputMatch(t *testing.T, app *cli.App, expected *regexp.Regexp) {
	buf, err := GetBuf(app)

	if err != nil {
		t.Errorf("Couldn't recover the buffer - use BaseTestSetup to create your cli.Apps")
		return
	}
	if !expected.Match(buf.Bytes()) {
		return
	}
}

// BaseTestSetup constructs mock config and client and produces a cli.App with the given commands.
func BaseTestSetup(t *testing.T, admin bool, commands []cli.Command) (conf *mocks.Config, client *mocks.Client, cliapp *cli.App) {
	cli.OsExiter = func(_ int) {}
	conf = new(mocks.Config)
	client = new(mocks.Client)
	conf.When("GetBool", "admin").Return(admin, nil)
	conf.When("GetV", "output-format").Return(config.Var{"output-format", "human", "CODE"})

	cliapp, err := app.BaseAppSetup(app.GlobalFlags(), commands)
{'fraction_non_alphanumeric': 0.13126810786717183, 'fraction_numerical': 0.0, 'mean_word_length': 4.194444444444445, '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': '4485800', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1521, 'n_words': 444}