<commit_before>package main

import (
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"reflect"
	"strings"
	"testing"
)

func TestMain(t *testing.T) {
	defer func() {
		log.SetOutput(os.Stderr)
	}()
	log.SetOutput(ioutil.Discard)

	runMainHelpTestCase(t, "-h")
	runMainHelpTestCase(t, "--help")

	runMainExecTestCase(t, []string{})
	runMainExecTestCase(t, []string{"--test-flag"})
}

func runMainHelpTestCase(t *testing.T, flag string) {
	oldPepperFlashDir := pepperFlashDir
	oldExecCommand := execCommand
	oldArgs := os.Args
	oldStdout := os.Stdout
	defer func() {
		pepperFlashDir = oldPepperFlashDir
		execCommand = oldExecCommand
		os.Args = oldArgs
		os.Stdout = oldStdout
	}()

	pepperFlashDir = "testdata"
	execCommand = func(string, []string, []string) error { return nil }
	os.Args = []string{"RunLauncher()", flag}
	r, w, _ := os.Pipe()
	os.Stdout = w

	if err := RunLauncher(); err != 0 {
		t.Fatalf("%s exited with status code %d instead of 0", os.Args, err)
	}

	w.Close()
	out, _ := ioutil.ReadAll(r)

	for _, expected := range []string{"Currently detected flags", "PepperFlash support"} {
		if !strings.Contains(string(out), "Chromium launcher") {
			t.Fatalf("Can't find %q in --help output", expected)
		}
	}
}

func runMainExecTestCase(t *testing.T, flags []string) {
	oldPepperFlashDir := pepperFlashDir
	oldExecCommand := execCommand
	oldArgs := os.Args
	oldConfigHome := os.Getenv("XDG_CONFIG_HOME")
	defer func() {
		pepperFlashDir = oldPepperFlashDir
		execCommand = oldExecCommand
		os.Args = oldArgs
		os.Setenv("XDG_CONFIG_HOME", oldConfigHome)
	}()

	pepperFlashDir = "testdata"
	var execArgs []string
	var execEnv []string
	execCommand = func(argv0 string, argv []string, envv []string) error {
		execArgs = []string{argv0}
		execArgs = append(execArgs, argv...)
		execEnv = envv
		return nil
	}
	os.Args = []string{"RunLauncher()"}
	os.Args = append(os.Args, flags...)
	os.Setenv("XDG_CONFIG_HOME", "testdata")

	expectedArgs := []string{
		chromiumBinary, chromiumBinary, "--ppapi-flash-version=4.5.6",
		"--ppapi-flash-path=testdata/libpepflashplayer.so"}
	expectedArgs = append(expectedArgs, "--if", "--it", "--builds", "--it --ships")
	expectedArgs = append(expectedArgs, flags...)

	if err := RunLauncher(); err != 1 {<commit_msg>Remove unused variable execEnv<commit_after>package main

import (
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"reflect"
	"strings"
	"testing"
)

func TestMain(t *testing.T) {
	defer func() {
		log.SetOutput(os.Stderr)
	}()
	log.SetOutput(ioutil.Discard)

	runMainHelpTestCase(t, "-h")
	runMainHelpTestCase(t, "--help")

	runMainExecTestCase(t, []string{})
	runMainExecTestCase(t, []string{"--test-flag"})
}

func runMainHelpTestCase(t *testing.T, flag string) {
	oldPepperFlashDir := pepperFlashDir
	oldExecCommand := execCommand
	oldArgs := os.Args
	oldStdout := os.Stdout
	defer func() {
		pepperFlashDir = oldPepperFlashDir
		execCommand = oldExecCommand
		os.Args = oldArgs
		os.Stdout = oldStdout
	}()

	pepperFlashDir = "testdata"
	execCommand = func(string, []string, []string) error { return nil }
	os.Args = []string{"RunLauncher()", flag}
	r, w, _ := os.Pipe()
	os.Stdout = w

	if err := RunLauncher(); err != 0 {
		t.Fatalf("%s exited with status code %d instead of 0", os.Args, err)
	}

	w.Close()
	out, _ := ioutil.ReadAll(r)

	for _, expected := range []string{"Currently detected flags", "PepperFlash support"} {
		if !strings.Contains(string(out), "Chromium launcher") {
			t.Fatalf("Can't find %q in --help output", expected)
		}
	}
}

func runMainExecTestCase(t *testing.T, flags []string) {
	oldPepperFlashDir := pepperFlashDir
	oldExecCommand := execCommand
	oldArgs := os.Args
	oldConfigHome := os.Getenv("XDG_CONFIG_HOME")
	defer func() {
		pepperFlashDir = oldPepperFlashDir
		execCommand = oldExecCommand
		os.Args = oldArgs
		os.Setenv("XDG_CONFIG_HOME", oldConfigHome)
	}()

	pepperFlashDir = "testdata"
	var execArgs []string
	execCommand = func(argv0 string, argv []string, envv []string) error {
		execArgs = []string{argv0}
		execArgs = append(execArgs, argv...)
		return nil
	}
	os.Args = []string{"RunLauncher()"}
	os.Args = append(os.Args, flags...)
	os.Setenv("XDG_CONFIG_HOME", "testdata")

	expectedArgs := []string{
		chromiumBinary, chromiumBinary, "--ppapi-flash-version=4.5.6",
		"--ppapi-flash-path=testdata/libpepflashplayer.so"}
	expectedArgs = append(expectedArgs, "--if", "--it", "--builds", "--it --ships")
	expectedArgs = append(expectedArgs, flags...)

	if err := RunLauncher(); err != 1 {
{'fraction_non_alphanumeric': 0.16029346376167186, 'fraction_numerical': 0.0035571365051133837, 'mean_word_length': 4.506731946144431, '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': '3041458', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1618, 'n_words': 356}