<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"testing"

	"github.com/hashicorp/packer/packer"
)

func TestDecodeConfig(t *testing.T) {

	packerConfig := `
	{
		"PluginMinPort": 10,
		"PluginMaxPort": 25,
		"disable_checkpoint": true,
		"disable_checkpoint_signature": true,
		"provisioners": {
		    "super-shell": "packer-provisioner-super-shell"
		}
	}`

	var cfg config
	err := decodeConfig(strings.NewReader(packerConfig), &cfg)
	if err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	var expectedCfg config
	json.NewDecoder(strings.NewReader(packerConfig)).Decode(&expectedCfg)
	if !reflect.DeepEqual(cfg, expectedCfg) {
		t.Errorf("failed to load custom configuration data; expected %v got %v", expectedCfg, cfg)
	}

}

func TestLoadExternalComponentsFromConfig(t *testing.T) {
	packerConfigData, cleanUpFunc, err := generateFakePackerConfigData()
	if err != nil {
		t.Fatalf("error encountered while creating fake Packer configuration data %v", err)
	}
	defer cleanUpFunc()

	var cfg config
	cfg.Builders = packer.MapOfBuilder{}
	cfg.PostProcessors = packer.MapOfPostProcessor{}
	cfg.Provisioners = packer.MapOfProvisioner{}

	if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	cfg.LoadExternalComponentsFromConfig()

	if len(cfg.Builders) != 1 || !cfg.Builders.Has("cloud-xyz") {
		t.Errorf("failed to load external builders; got %v as the resulting config", cfg.Builders)
	}

	if len(cfg.PostProcessors) != 1 || !cfg.PostProcessors.Has("noop") {
		t.Errorf("failed to load external post-processors; got %v as the resulting config", cfg.PostProcessors)
	}

	if len(cfg.Provisioners) != 1 || !cfg.Provisioners.Has("super-shell") {
		t.Errorf("failed to load external provisioners; got %v as the resulting config", cfg.Provisioners)
	}

}

func TestLoadExternalComponentsFromConfig_onlyProvisioner(t *testing.T) {
	packerConfigData, cleanUpFunc, err := generateFakePackerConfigData()
	if err != nil {
		t.Fatalf("error encountered while creating fake Packer configuration data %v", err)
	}
	defer cleanUpFunc()

	var cfg config
	cfg.Provisioners = packer.MapOfProvisioner{}

	if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	/* Let's clear out any custom Builders or PostProcessors that were part of the config.
	This step does not remove them from disk, it just removes them from of plugins Packer knows about.
	*/
	cfg.RawBuilders = nil
	cfg.RawPostProcessors = nil

	cfg.LoadExternalComponentsFromConfig()

	if len(cfg.Builders) != 0 {
		t.Errorf("loaded external builders when it wasn't supposed to; got %v as the resulting config", cfg.Builders)
	}

	if len(cfg.PostProcessors) != 0 {
		t.Errorf("loaded external post-processors when it wasn't supposed to; got %v as the resulting config", cfg.PostProcessors)
	}

	if len(cfg.Provisioners) != 1 || !cfg.Provisioners.Has("super-shell") {
		t.Errorf("failed to load external provisioners; got %v as the resulting config", cfg.Provisioners)
	}
}

func TestLoadSingleComponent(t *testing.T) {

	tmpFile, err := ioutil.TempFile(".", "packer-builder-")
	if err != nil {
		t.Fatalf("failed to create test file with error: %s", err)
	}
	defer os.Remove(tmpFile.Name())

	tt := []struct {
		pluginPath    string
		errorExpected bool
	}{
		{pluginPath: tmpFile.Name(), errorExpected: false},
		{pluginPath: "./non-existing-file", errorExpected: true},
	}

	var cfg config
	cfg.Builders = packer.MapOfBuilder{}
	cfg.PostProcessors = packer.MapOfPostProcessor{}
	cfg.Provisioners = packer.MapOfProvisioner{}

	for _, tc := range tt {
		tc := tc
		_, err := cfg.loadSingleComponent(tc.pluginPath)
		if tc.errorExpected && err == nil {
			t.Errorf("expected loadSingleComponent(%s) to error but it didn't", tc.pluginPath)<commit_msg>Update test case for Windows<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"testing"

	"github.com/hashicorp/packer/packer"
)

func TestDecodeConfig(t *testing.T) {

	packerConfig := `
	{
		"PluginMinPort": 10,
		"PluginMaxPort": 25,
		"disable_checkpoint": true,
		"disable_checkpoint_signature": true,
		"provisioners": {
		    "super-shell": "packer-provisioner-super-shell"
		}
	}`

	var cfg config
	err := decodeConfig(strings.NewReader(packerConfig), &cfg)
	if err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	var expectedCfg config
	json.NewDecoder(strings.NewReader(packerConfig)).Decode(&expectedCfg)
	if !reflect.DeepEqual(cfg, expectedCfg) {
		t.Errorf("failed to load custom configuration data; expected %v got %v", expectedCfg, cfg)
	}

}

func TestLoadExternalComponentsFromConfig(t *testing.T) {
	packerConfigData, cleanUpFunc, err := generateFakePackerConfigData()
	if err != nil {
		t.Fatalf("error encountered while creating fake Packer configuration data %v", err)
	}
	defer cleanUpFunc()

	var cfg config
	cfg.Builders = packer.MapOfBuilder{}
	cfg.PostProcessors = packer.MapOfPostProcessor{}
	cfg.Provisioners = packer.MapOfProvisioner{}

	if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	cfg.LoadExternalComponentsFromConfig()

	if len(cfg.Builders) != 1 || !cfg.Builders.Has("cloud-xyz") {
		t.Errorf("failed to load external builders; got %v as the resulting config", cfg.Builders)
	}

	if len(cfg.PostProcessors) != 1 || !cfg.PostProcessors.Has("noop") {
		t.Errorf("failed to load external post-processors; got %v as the resulting config", cfg.PostProcessors)
	}

	if len(cfg.Provisioners) != 1 || !cfg.Provisioners.Has("super-shell") {
		t.Errorf("failed to load external provisioners; got %v as the resulting config", cfg.Provisioners)
	}

}

func TestLoadExternalComponentsFromConfig_onlyProvisioner(t *testing.T) {
	packerConfigData, cleanUpFunc, err := generateFakePackerConfigData()
	if err != nil {
		t.Fatalf("error encountered while creating fake Packer configuration data %v", err)
	}
	defer cleanUpFunc()

	var cfg config
	cfg.Provisioners = packer.MapOfProvisioner{}

	if err := decodeConfig(strings.NewReader(packerConfigData), &cfg); err != nil {
		t.Fatalf("error encountered decoding configuration: %v", err)
	}

	/* Let's clear out any custom Builders or PostProcessors that were part of the config.
	This step does not remove them from disk, it just removes them from of plugins Packer knows about.
	*/
	cfg.RawBuilders = nil
	cfg.RawPostProcessors = nil

	cfg.LoadExternalComponentsFromConfig()

	if len(cfg.Builders) != 0 {
		t.Errorf("loaded external builders when it wasn't supposed to; got %v as the resulting config", cfg.Builders)
	}

	if len(cfg.PostProcessors) != 0 {
		t.Errorf("loaded external post-processors when it wasn't supposed to; got %v as the resulting config", cfg.PostProcessors)
	}

	if len(cfg.Provisioners) != 1 || !cfg.Provisioners.Has("super-shell") {
		t.Errorf("failed to load external provisioners; got %v as the resulting config", cfg.Provisioners)
	}
}

func TestLoadSingleComponent(t *testing.T) {

	// .exe will work everyone for testing purpose, but mostly here to help Window's test runs.
	tmpFile, err := ioutil.TempFile(".", "packer-builder-*.exe")
	if err != nil {
		t.Fatalf("failed to create test file with error: %s", err)
	}
	defer os.Remove(tmpFile.Name())

	tt := []struct {
		pluginPath    string
		errorExpected bool
	}{
		{pluginPath: tmpFile.Name(), errorExpected: false},
		{pluginPath: "./non-existing-file", errorExpected: true},
	}

	var cfg config
	cfg.Builders = packer.MapOfBuilder{}
	cfg.PostProcessors = packer.MapOfPostProcessor{}
	cfg.Provisioners = packer.MapOfProvisioner{}

	for _, tc := range tt {
		tc := tc
		_, err := cfg.loadSingleComponent(tc.pluginPath)
		if tc.errorExpected && err == nil {
			t.Errorf("expected loadSingleComponent(%s) to error but it didn't", tc.pluginPath)
{'fraction_non_alphanumeric': 0.12538565963223497, 'fraction_numerical': 0.002468221646303838, 'mean_word_length': 4.743444365698086, 'pattern_counts': {'":': 12, '<': 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': '6292426', 'n_tokens_mistral': 2762, 'n_tokens_neox': 2633, 'n_words': 710}