<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 (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"
)

// TestStartCommandOK tests start.Command() returns immediately without waiting
// for the command to complete.
func TestStartCommandOK(t *testing.T) {
	var out bytes.Buffer
	start := NewStart(nil, os.Stdin, &out, ioutil.Discard, false, false, true)
	bin, err := buildTestProgram(NewRun(nil, os.Stdin, &out, ioutil.Discard, false, false, true), "slow_hello2")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	if _, err := start.Command(bin); err != nil {
		t.Fatalf(`Command("go run ./testdata/slow_hello2.go") failed: %v`, err)
	}
	// Note that the output shouldn't have "hello" because start.Command won't
	// wait for the command to finish.
	if got, want := removeTimestamps(t, &out), fmt.Sprintf(">> %s\n>> OK\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

// TestStartCommandWithNonZeroExitCodeOK tests that start.Command() succeeds
// even if the command it started failed.
func TestStartCommandWithNonZeroExitCodeOK(t *testing.T) {
	var out bytes.Buffer
	start := NewStart(nil, os.Stdin, &out, ioutil.Discard, false, false, true)
	if _, err := start.Command("go", "run", "./testdata/fail_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/fail_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/fail_hello.go\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestStartCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	start := NewStart(nil, os.Stdin, &runOut, ioutil.Discard, false, false, true)
	opts := start.Opts()
	opts.Stdout = &cmdOut
	if _, err := start.CommandWithOpts(opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	time.Sleep(time.Second * 3)
	if got, want := removeTimestamps(t, &cmdOut), "hello\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}
<commit_msg>TBR: devtools/internal/runutil: trying to fix TestStartCommandOK<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 (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"
)

// TestStartCommandOK tests start.Command() returns immediately without waiting
// for the command to complete.
func TestStartCommandOK(t *testing.T) {
	var out bytes.Buffer
	start := NewStart(nil, os.Stdin, &out, ioutil.Discard, false, false, true)
	bin, err := buildTestProgram(NewRun(nil, os.Stdin, &out, ioutil.Discard, false, false, true), "slow_hello2")
	if bin != "" {
		defer os.RemoveAll(filepath.Dir(bin))
	}
	if err != nil {
		t.Fatalf("%v", err)
	}
	if _, err := start.Command(bin); err != nil {
		t.Fatalf(`Command("go run ./testdata/slow_hello2.go") failed: %v`, err)
	}
	time.Sleep(time.Second * 3)
	// Note that the output shouldn't have "hello" because start.Command won't
	// wait for the command to finish.
	if got, want := removeTimestamps(t, &out), fmt.Sprintf(">> %s\n>> OK\n", bin); got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

// TestStartCommandWithNonZeroExitCodeOK tests that start.Command() succeeds
// even if the command it started failed.
func TestStartCommandWithNonZeroExitCodeOK(t *testing.T) {
	var out bytes.Buffer
	start := NewStart(nil, os.Stdin, &out, ioutil.Discard, false, false, true)
	if _, err := start.Command("go", "run", "./testdata/fail_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/fail_hello.go") failed: %v`, err)
	}
	if got, want := removeTimestamps(t, &out), ">> go run ./testdata/fail_hello.go\n>> OK\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

func TestStartCommandWithOptsOK(t *testing.T) {
	var cmdOut, runOut bytes.Buffer
	start := NewStart(nil, os.Stdin, &runOut, ioutil.Discard, false, false, true)
	opts := start.Opts()
	opts.Stdout = &cmdOut
	if _, err := start.CommandWithOpts(opts, "go", "run", "./testdata/ok_hello.go"); err != nil {
		t.Fatalf(`Command("go run ./testdata/ok_hello.go") failed: %v`, err)
	}
	time.Sleep(time.Second * 3)
	if got, want := removeTimestamps(t, &cmdOut), "hello\n"; got != want {
		t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v", got, want)
	}
}

{'fraction_non_alphanumeric': 0.1673773987206823, 'fraction_numerical': 0.0031982942430703624, 'mean_word_length': 4.223830734966592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2243911', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1729, 'n_words': 459}