<commit_before>package henchman

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

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

func writeTempFile(buf []byte, fname string) string {
	fpath := path.Join("/tmp", fname)
	ioutil.WriteFile(fpath, buf, 0644)
	return fpath
}

func rmTempFile(fpath string) {
	os.Remove(fpath)
}

func TestPreprocessInventoryAtHostLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/inventoryAtHostLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 4, len(plan.Tasks), "Wrong number of tasks.")
	// NOTE: The inner hosts are ignored and the top level is taken
	assert.Equal(t, 2, plan.Inventory.Count(), "Wrong number of machines")
}

func TestPreprocessIncludeAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 3, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "task1", plan.Tasks[0].Name, "Wrong first task.")
	assert.Equal(t, "included_task1", plan.Tasks[1].Name, "Wrong second task.")
}

func TestPreprocessNestedIncludeAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/nestedIncludeAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 4, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "task1", plan.Tasks[0].Name, "Wrong first task.")
	assert.Equal(t, "included_task1", plan.Tasks[2].Name, "Wrong second task.")
}

func TestPreprocessIncludeAndVarsAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAndVarsAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 6, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "bar", plan.Tasks[0].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "nope", plan.Tasks[1].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "thumb", plan.Tasks[2].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "nope", plan.Tasks[4].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "bar", plan.Tasks[5].Vars["foo"], "Wrong key in Task Vars")
}

func TestPreprocessIncludeAtVarsLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAtVarsLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	require.Equal(t, 5, len(plan.Vars), "Wrong number of vars.")

	for key, val := range plan.Vars {
		switch key {
		case "fun":
			assert.Equal(t, "times", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "hello":
			assert.Equal(t, "world", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "foo":
			assert.Equal(t, "scar", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "spam":
			assert.Equal(t, "eggs", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "goodbye":
			assert.Equal(t, "moon", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		}
	}
}

func TestPreprocessIncludeAndWhenAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAndWhenAtTaskLevel.yaml")
	require.NoError(t, err)
<commit_msg>Fix test case in preprocessor<commit_after>package henchman

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

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

func writeTempFile(buf []byte, fname string) string {
	fpath := path.Join("/tmp", fname)
	ioutil.WriteFile(fpath, buf, 0644)
	return fpath
}

func rmTempFile(fpath string) {
	os.Remove(fpath)
}

func TestPreprocessInventoryAtHostLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/inventoryAtHostLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 4, len(plan.Tasks), "Wrong number of tasks.")
	// NOTE: The inner hosts are ignored and the top level is taken
	assert.Equal(t, 2, plan.Inventory.Count(), "Wrong number of machines")
}

func TestPreprocessIncludeAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 3, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "task1", plan.Tasks[0].Name, "Wrong first task.")
	assert.Equal(t, "included_task1", plan.Tasks[1].Name, "Wrong second task.")
}

func TestPreprocessNestedIncludeAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/nestedIncludeAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 4, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "task1", plan.Tasks[0].Name, "Wrong first task.")
	assert.Equal(t, "included_task1", plan.Tasks[2].Name, "Wrong second task.")
}

func TestPreprocessIncludeAndVarsAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAndVarsAtTaskLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	assert.Equal(t, 6, len(plan.Tasks), "Wrong number of tasks.")
	assert.Equal(t, "bar", plan.Tasks[0].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "nope", plan.Tasks[1].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "thumb", plan.Tasks[2].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "nope", plan.Tasks[4].Vars["foo"], "Wrong key in Task Vars")
	assert.Equal(t, "bar", plan.Tasks[5].Vars["foo"], "Wrong key in Task Vars")
}

func TestPreprocessIncludeAtVarsLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAtVarsLevel.yaml")
	require.NoError(t, err)

	plan, err := PreprocessPlan(buf, inv)
	require.NoError(t, err)

	require.Equal(t, 6, len(plan.Vars), "Wrong number of vars.")

	for key, val := range plan.Vars {
		switch key {
		case "fun":
			assert.Equal(t, "times", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "hello":
			assert.Equal(t, "world", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "foo":
			assert.Equal(t, "scar", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "spam":
			assert.Equal(t, "eggs", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		case "goodbye":
			assert.Equal(t, "moon", val.(string), fmt.Sprintf("Wrong value for key %v", key))
		}
	}
}

func TestPreprocessIncludeAndWhenAtTaskLevel(t *testing.T) {
	inv, _ := loadValidInventory()
	buf, err := ioutil.ReadFile("test/plan/includeAndWhenAtTaskLevel.yaml")
	require.NoError(t, err)

{'fraction_non_alphanumeric': 0.16997729852440407, 'fraction_numerical': 0.0065266742338251985, 'mean_word_length': 5.124239791485665, 'pattern_counts': {'":': 10, '<': 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': '6117643', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2641, 'n_words': 600}