<commit_before>package fs

import (
	"bytes"
	"crypto/rand"
	"io/ioutil"
	"os"
	"testing"

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

func TestObscure(t *testing.T) {
	for _, test := range []struct {
		in   string
		want string
		iv   string
	}{
		{"", "YWFhYWFhYWFhYWFhYWFhYQ", "aaaaaaaaaaaaaaaa"},
		{"potato", "YWFhYWFhYWFhYWFhYWFhYXMaGgIlEQ", "aaaaaaaaaaaaaaaa"},
		{"potato", "YmJiYmJiYmJiYmJiYmJiYp3gcEWbAw", "bbbbbbbbbbbbbbbb"},
	} {
		cryptRand = bytes.NewBufferString(test.iv)
		got, err := Obscure(test.in)
		cryptRand = rand.Reader
		assert.NoError(t, err)
		assert.Equal(t, test.want, got)
		recoveredIn, err := Reveal(got)
		assert.NoError(t, err)
		assert.Equal(t, test.in, recoveredIn, "not bidirectional")
		// Now the Must variants
		cryptRand = bytes.NewBufferString(test.iv)
		got = MustObscure(test.in)
		cryptRand = rand.Reader
		assert.Equal(t, test.want, got)
		recoveredIn = MustReveal(got)
		assert.Equal(t, test.in, recoveredIn, "not bidirectional")

	}
}

func TestCRUD(t *testing.T) {
	configKey = nil // reset password
	// create temp config file
	tempFile, err := ioutil.TempFile("", "crud.conf")
	assert.NoError(t, err)
	path := tempFile.Name()
	defer func() {
		err := os.Remove(path)
		assert.NoError(t, err)
	}()
	assert.NoError(t, tempFile.Close())

	// temporarily adapt configuration
	oldOsStdout := os.Stdout
	oldConfigFile := configFile
	oldConfig := Config
	oldReadLine := ReadLine
	os.Stdout = nil
	configFile = &path
	Config = &ConfigInfo{}
	defer func() {
		os.Stdout = oldOsStdout
		configFile = oldConfigFile
		ReadLine = oldReadLine
		Config = oldConfig
	}()

	LoadConfig()
	assert.Equal(t, []string{}, configData.GetSectionList())

	// add new remote
	i := 0
	ReadLine = func() string {
		answers := []string{
			"local", // type is local
			"1",     // yes, disable long filenames
			"y",     // looks good, save
		}
		i = i + 1
		return answers[i-1]
	}
	NewRemote("test")
	assert.Equal(t, []string{"test"}, configData.GetSectionList())

	// normal rename, test → asdf
	ReadLine = func() string { return "asdf" }
	RenameRemote("test")
	assert.Equal(t, []string{"asdf"}, configData.GetSectionList())
<commit_msg>Fix config tests to save configData which fixes subsequent tests<commit_after>package fs

import (
	"bytes"
	"crypto/rand"
	"io/ioutil"
	"os"
	"testing"

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

func TestObscure(t *testing.T) {
	for _, test := range []struct {
		in   string
		want string
		iv   string
	}{
		{"", "YWFhYWFhYWFhYWFhYWFhYQ", "aaaaaaaaaaaaaaaa"},
		{"potato", "YWFhYWFhYWFhYWFhYWFhYXMaGgIlEQ", "aaaaaaaaaaaaaaaa"},
		{"potato", "YmJiYmJiYmJiYmJiYmJiYp3gcEWbAw", "bbbbbbbbbbbbbbbb"},
	} {
		cryptRand = bytes.NewBufferString(test.iv)
		got, err := Obscure(test.in)
		cryptRand = rand.Reader
		assert.NoError(t, err)
		assert.Equal(t, test.want, got)
		recoveredIn, err := Reveal(got)
		assert.NoError(t, err)
		assert.Equal(t, test.in, recoveredIn, "not bidirectional")
		// Now the Must variants
		cryptRand = bytes.NewBufferString(test.iv)
		got = MustObscure(test.in)
		cryptRand = rand.Reader
		assert.Equal(t, test.want, got)
		recoveredIn = MustReveal(got)
		assert.Equal(t, test.in, recoveredIn, "not bidirectional")

	}
}

func TestCRUD(t *testing.T) {
	configKey = nil // reset password
	// create temp config file
	tempFile, err := ioutil.TempFile("", "crud.conf")
	assert.NoError(t, err)
	path := tempFile.Name()
	defer func() {
		err := os.Remove(path)
		assert.NoError(t, err)
	}()
	assert.NoError(t, tempFile.Close())

	// temporarily adapt configuration
	oldOsStdout := os.Stdout
	oldConfigFile := configFile
	oldConfig := Config
	oldConfigData := configData
	oldReadLine := ReadLine
	os.Stdout = nil
	configFile = &path
	Config = &ConfigInfo{}
	configData = nil
	defer func() {
		os.Stdout = oldOsStdout
		configFile = oldConfigFile
		ReadLine = oldReadLine
		Config = oldConfig
		configData = oldConfigData
	}()

	LoadConfig()
	assert.Equal(t, []string{}, configData.GetSectionList())

	// add new remote
	i := 0
	ReadLine = func() string {
		answers := []string{
			"local", // type is local
			"1",     // yes, disable long filenames
			"y",     // looks good, save
		}
		i = i + 1
		return answers[i-1]
	}
	NewRemote("test")
	assert.Equal(t, []string{"test"}, configData.GetSectionList())

	// normal rename, test → asdf
	ReadLine = func() string { return "asdf" }
	RenameRemote("test")
	assert.Equal(t, []string{"asdf"}, configData.GetSectionList())

{'fraction_non_alphanumeric': 0.14782031422881167, 'fraction_numerical': 0.002212878955521133, 'mean_word_length': 4.050279329608938, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '3051258', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1716, 'n_words': 369}