<commit_before>package eval_test

import (
	"errors"
	"testing"
	"time"

	. "github.com/elves/elvish/pkg/eval"

	. "github.com/elves/elvish/pkg/eval/evaltest"
	"github.com/elves/elvish/pkg/testutil"
)

func TestBuiltinFnMisc(t *testing.T) {
	Test(t,
		That(`f = (constantly foo); $f; $f`).Puts("foo", "foo"),

		That("eval 'put x'").Puts("x"),
		// Using initial binding in &ns.
		That("n = (ns [&x=foo]); eval 'put $x' &ns=$n").Puts("foo"),
		// Altering variables in &ns.
		That("n = (ns [&x=foo]); eval 'x = bar' &ns=$n; put $n[x]").Puts("bar"),
		// Parse error.
		That("eval '['").Throws(AnyError),
		// Compilation error.
		That("eval 'put $x'").Throws(AnyError),
		// Exception.
		That("eval 'fail x'").Throws(FailError{"x"}),

		That(`f = (mktemp elvXXXXXX); echo 'put x' > $f
		      -source $f; rm $f`).Puts("x"),
		That(`f = (mktemp elvXXXXXX); echo 'put $x' > $f
		      fn p [x]{ -source $f }; p x; rm $f`).Puts("x"),

		// Test the "time" builtin.
		//
		// Since runtime duration is non-deterministic, we only have some sanity
		// checks here.
		That("time { echo foo } | a _ = (all)", "put $a").Puts("foo"),
		That("duration = ''",
			"time &on-end=[x]{ duration = $x } { echo foo } | out = (all)",
			"put $out", "kind-of $duration").Puts("foo", "number"),
		That("time { fail body } | nop (all)").Throws(FailError{"body"}),
		That("time &on-end=[_]{ fail on-end } { }").Throws(
			FailError{"on-end"}),

		That("time &on-end=[_]{ fail on-end } { fail body }").Throws(
			FailError{"body"}),
	)
}<commit_msg>pkg/eval: Simplify the tests for -source by running them in a temp dir.<commit_after>package eval_test

import (
	"errors"
	"testing"
	"time"

	. "github.com/elves/elvish/pkg/eval"

	. "github.com/elves/elvish/pkg/eval/evaltest"
	"github.com/elves/elvish/pkg/testutil"
)

func TestBuiltinFnMisc(t *testing.T) {
	Test(t,
		That(`f = (constantly foo); $f; $f`).Puts("foo", "foo"),

		That("eval 'put x'").Puts("x"),
		// Using initial binding in &ns.
		That("n = (ns [&x=foo]); eval 'put $x' &ns=$n").Puts("foo"),
		// Altering variables in &ns.
		That("n = (ns [&x=foo]); eval 'x = bar' &ns=$n; put $n[x]").Puts("bar"),
		// Parse error.
		That("eval '['").Throws(AnyError),
		// Compilation error.
		That("eval 'put $x'").Throws(AnyError),
		// Exception.
		That("eval 'fail x'").Throws(FailError{"x"}),

		// Test the "time" builtin.
		//
		// Since runtime duration is non-deterministic, we only have some sanity
		// checks here.
		That("time { echo foo } | a _ = (all)", "put $a").Puts("foo"),
		That("duration = ''",
			"time &on-end=[x]{ duration = $x } { echo foo } | out = (all)",
			"put $out", "kind-of $duration").Puts("foo", "number"),
		That("time { fail body } | nop (all)").Throws(FailError{"body"}),
		That("time &on-end=[_]{ fail on-end } { }").Throws(
			FailError{"on-end"}),

		That("time &on-end=[_]{ fail on-end } { fail body }").Throws(
			FailError{"body"}),
	)
}

func TestSource(t *testing.T) {
	_, cleanup := testutil.InTestDir()
	defer cleanup()

	Test(t,
		That("echo 'put x' > a.elv; -source a.elv").Puts("x"),
		That("echo 'put $x' > a.elv; fn p [x]{ -source a.elv }; p foo").Puts("foo"),
	)
}
{'fraction_non_alphanumeric': 0.2384126984126984, 'fraction_numerical': 0.0, 'mean_word_length': 3.488603988603989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3606733', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1249, 'n_words': 296}