<commit_before>package hooks

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"config"
	"io/ioutil"
)

type hookTestCase struct {
	hookDef        *config.HookDef
	input          string
	expected       string
	errorValidator func(error) error
}

func noError(err error) error {
	return err
}

func TestHooks(t *testing.T) {

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.String() == "/bad" {
			w.WriteHeader(500)
			fmt.Fprintln(w, "bad response")
		}

		if r.URL.String() == "/test/web.txt" {
			w.WriteHeader(200)
			fmt.Fprintln(w, "RES:web.txt")
			return
		}

	}))
	defer ts.Close()

	var hookTestCases = []hookTestCase{
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "file_fixture.txt",
			},
			"anything",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "$0",
			},
			"../file_fixture.txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   "^extension:(.*)$",
				Template: "file_fixture.$1",
			},
			"extension:txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   ".*",
				Template: "echo shellhello",
			},
			"anything",
			"shellhello",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:haha",
			"haha",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$(hostname)",
			"$(hostname)",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:`hostname`",
			"`hostname`",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$HOME",
			"$HOME",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "(.*)",
				Template: "echo $1; exit 1",
			},
			"foo",
			"foo",
			noError,
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/test/$1",
			},
			"url/web.txt",
			"RES:web.txt",
			noError,
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/bad",
			},
			"url/bad.txt",
			"bad response",
			func(err error) error {
				if err == nil {
					return fmt.Errorf("Bad url response test failed: Expected to have an error")
				}
				return nil
			},
		},
	}

	for _, testCase := range hookTestCases {
		hook, err := CompileHook(testCase.hookDef)
		if err != nil {
			t.Error("Failed to compile", testCase.hookDef, err)
			return
		}

		file, err := hook(testCase.input)
		if err == NO_MATCH {
			t.Error(testCase.hookDef.Regexp, "does not match with", testCase.input)
		}

		if err = testCase.errorValidator(err); err != nil {
			t.Error("Failed to execute hook:", testCase.hookDef, err)
			return
		}

		if file == nil {
			return<commit_msg>add length argument to test<commit_after>package hooks

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"config"
	"io/ioutil"
)

type hookTestCase struct {
	hookDef        *config.HookDef
	input          string
	expected       string
	errorValidator func(error) error
}

func noError(err error) error {
	return err
}

func TestHooks(t *testing.T) {

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.String() == "/bad" {
			w.WriteHeader(500)
			fmt.Fprintln(w, "bad response")
		}

		if r.URL.String() == "/test/web.txt" {
			w.WriteHeader(200)
			fmt.Fprintln(w, "RES:web.txt")
			return
		}

	}))
	defer ts.Close()

	var hookTestCases = []hookTestCase{
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "file_fixture.txt",
			},
			"anything",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "$0",
			},
			"../file_fixture.txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   "^extension:(.*)$",
				Template: "file_fixture.$1",
			},
			"extension:txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   ".*",
				Template: "echo shellhello",
			},
			"anything",
			"shellhello",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:haha",
			"haha",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$(hostname)",
			"$(hostname)",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:`hostname`",
			"`hostname`",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$HOME",
			"$HOME",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "(.*)",
				Template: "echo $1; exit 1",
			},
			"foo",
			"foo",
			noError,
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/test/$1",
			},
			"url/web.txt",
			"RES:web.txt",
			noError,
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/bad",
			},
			"url/bad.txt",
			"bad response",
			func(err error) error {
				if err == nil {
					return fmt.Errorf("Bad url response test failed: Expected to have an error")
				}
				return nil
			},
		},
	}

	for _, testCase := range hookTestCases {
		hook, err := CompileHook(testCase.hookDef)
		if err != nil {
			t.Error("Failed to compile", testCase.hookDef, err)
			return
		}

		file, _, err := hook(testCase.input)
		if err == NO_MATCH {
			t.Error(testCase.hookDef.Regexp, "does not match with", testCase.input)
		}

		if err = testCase.errorValidator(err); err != nil {
			t.Error("Failed to execute hook:", testCase.hookDef, err)
			return
		}

		if file == nil {
			return
{'fraction_non_alphanumeric': 0.1876933094579196, 'fraction_numerical': 0.0048836073579684195, 'mean_word_length': 2.2996777658431795, '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': '4476953', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2395, 'n_words': 469}