<commit_before>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runutil

import (
	"bufio"
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

const timedCommandTimeout = 3 * time.Second

var forever time.Duration

func removeTimestamps(t *testing.T, buffer *bytes.Buffer) string {
	result := ""
	scanner := bufio.NewScanner(buffer)
	for scanner.Scan() {
		line := scanner.Text()
		if index := strings.Index(line, prefix); index != -1 {
			result += line[index:] + "\n"
		} else {
			result += line + "\n"
		}
	}
	if err := scanner.Err(); err != nil {
		t.Fatalf("Scan() failed: %v", err)
	}
	return result
}

func TestCommandOK(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	if err := e.run(forever, e.opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/ok_hello.go\nhello\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestCommandFail(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	if err := e.run(forever, e.opts, "go", "run", "./testdata/fail_hello.go"); err == nil {
		t.Fatalf(`Command("go run ./testdata/fail_hello.go") did not fail when it should`)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/fail_hello.go\nhello\n>> FAILED\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(forever, opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`CommandWithOpts("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &runOut), ">> go run ./testdata/ok_hello.go\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestCommandWithOptsFail(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(forever, opts, "go", "run", "./testdata/fail_hello.go"); err == nil {
		t.Fatalf(`CommandWithOpts("go run ./testdata/fail_hello.go") did not fail when it should`)
	}
	if got, want := removeTimestamps(t, &runOut), ">> go run ./testdata/fail_hello.go\n>> FAILED\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestTimedCommandOK(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	if err := e.run(10*time.Second, e.opts, "go", "run", "./testdata/fast_hello.go"); err != nil {
		t.Fatalf(`TimedCommand("go run ./testdata/fast_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/fast_hello.go\nhello\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestTimedCommandFail(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "slow_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	if err := e.run(timedCommandTimeout, e.opts, bin); err == nil {
		t.Fatalf(`TimedCommand("go run ./testdata/slow_hello.go") did not fail when it should`)
	} else if got, want := IsTimeout(err), true; got != want {
		t.Fatalf("unexpected error: got %v, want %v", got, want)
	}
	if got, want := removeTimestamps(t, &out), fmt.Sprintf(">> %s\nhello\n>> TIMED OUT\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestTimedCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(10*time.Second, opts, "go", "run", "./testdata/fast_hello.go"); err != nil {
		t.Fatalf(`TimedCommandWithOpts("go run ./testdata/fast_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &runOut), ">> go run ./testdata/fast_hello.go\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestTimedCommandWithOptsFail(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "slow_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(timedCommandTimeout, opts, bin); err == nil {
		t.Fatalf(`TimedCommandWithOpts("go run ./testdata/slow_hello.go") did not fail when it should`)
	} else if got, want := IsTimeout(err), true; got != want {
		t.Fatalf("unexpected error: got %v, want %v", got, want)<commit_msg>TBR: jiri: increase timeout of TestTimedCommandOK.<commit_after>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runutil

import (
	"bufio"
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

const timedCommandTimeout = 3 * time.Second

var forever time.Duration

func removeTimestamps(t *testing.T, buffer *bytes.Buffer) string {
	result := ""
	scanner := bufio.NewScanner(buffer)
	for scanner.Scan() {
		line := scanner.Text()
		if index := strings.Index(line, prefix); index != -1 {
			result += line[index:] + "\n"
		} else {
			result += line + "\n"
		}
	}
	if err := scanner.Err(); err != nil {
		t.Fatalf("Scan() failed: %v", err)
	}
	return result
}

func TestCommandOK(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	if err := e.run(forever, e.opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/ok_hello.go\nhello\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestCommandFail(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	if err := e.run(forever, e.opts, "go", "run", "./testdata/fail_hello.go"); err == nil {
		t.Fatalf(`Command("go run ./testdata/fail_hello.go") did not fail when it should`)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/fail_hello.go\nhello\n>> FAILED\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(forever, opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`CommandWithOpts("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &runOut), ">> go run ./testdata/ok_hello.go\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestCommandWithOptsFail(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(forever, opts, "go", "run", "./testdata/fail_hello.go"); err == nil {
		t.Fatalf(`CommandWithOpts("go run ./testdata/fail_hello.go") did not fail when it should`)
	}
	if got, want := removeTimestamps(t, &runOut), ">> go run ./testdata/fail_hello.go\n>> FAILED\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestTimedCommandOK(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "fast_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	if err := e.run(2*time.Minute, e.opts, bin); err != nil {
		t.Fatalf(`TimedCommand("go run ./testdata/fast_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), fmt.Sprintf(">> %s\nhello\n>> OK\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestTimedCommandFail(t *testing.T) {
	var out bytes.Buffer
	e := newExecutor(nil, os.Stdin, &out, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "slow_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	if err := e.run(timedCommandTimeout, e.opts, bin); err == nil {
		t.Fatalf(`TimedCommand("go run ./testdata/slow_hello.go") did not fail when it should`)
	} else if got, want := IsTimeout(err), true; got != want {
		t.Fatalf("unexpected error: got %v, want %v", got, want)
	}
	if got, want := removeTimestamps(t, &out), fmt.Sprintf(">> %s\nhello\n>> TIMED OUT\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestTimedCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "fast_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(2*time.Minute, opts, bin); err != nil {
		t.Fatalf(`TimedCommandWithOpts("go run ./testdata/fast_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &runOut), fmt.Sprintf(">> %s\n>> OK\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
	if got, want := strings.TrimSpace(cmdOut.String()), "hello"; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}

func TestTimedCommandWithOptsFail(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	e := newExecutor(nil, os.Stdin, &runOut, ioutil.Discard, false, true)
	bin, err := buildTestProgram(e, "slow_hello")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	opts := e.opts
	opts.stdout = &cmdOut
	if err := e.run(timedCommandTimeout, opts, bin); err == nil {
		t.Fatalf(`TimedCommandWithOpts("go run ./testdata/slow_hello.go") did not fail when it should`)
	} else if got, want := IsTimeout(err), true; got != want {
		t.Fatalf("unexpected error: got %v, want %v", got, want)
{'fraction_non_alphanumeric': 0.18745111671156686, 'fraction_numerical': 0.0015642652298600852, 'mean_word_length': 4.067371202113606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, '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': '1844929', 'n_tokens_mistral': 4562, 'n_tokens_neox': 4520, 'n_words': 1118}