<commit_before>// Copyright 2017 The Go 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 archive

import (
	"bytes"
	"debug/elf"
	"debug/macho"
	"debug/pe"
	"fmt"
	"internal/testenv"
	"internal/xcoff"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sync"
	"testing"
	"unicode/utf8"
)

var buildDir string

func TestMain(m *testing.M) {
	if !testenv.HasGoBuild() {
		return
	}

	exit := m.Run()

	if buildDir != "" {
		os.RemoveAll(buildDir)
	}
	os.Exit(exit)
}

func copyDir(dst, src string) error {
	err := os.MkdirAll(dst, 0777)
	if err != nil {
		return err
	}
	entries, err := os.ReadDir(src)
	if err != nil {
		return err
	}
	for _, entry := range entries {
		err = copyFile(filepath.Join(dst, entry.Name()), filepath.Join(src, entry.Name()))
		if err != nil {
			return err
		}
	}
	return nil
}

func copyFile(dst, src string) (err error) {
	var s, d *os.File
	s, err = os.Open(src)
	if err != nil {
		return err
	}
	defer s.Close()
	d, err = os.Create(dst)
	if err != nil {
		return err
	}
	defer func() {
		e := d.Close()
		if err == nil {
			err = e
		}
	}()
	_, err = io.Copy(d, s)
	if err != nil {
		return err
	}
	return nil
}

var (
	buildOnce   sync.Once
	builtGoobjs goobjPaths
	buildErr    error
)

type goobjPaths struct {
	go1obj     string
	go2obj     string
	goarchive  string
	cgoarchive string
}

func buildGoobj(t *testing.T) goobjPaths {
	buildOnce.Do(func() {
		buildErr = func() (err error) {
			buildDir, err = os.MkdirTemp("", "TestGoobj")
			if err != nil {
				return err
			}

			go1obj := filepath.Join(buildDir, "go1.o")
			go2obj := filepath.Join(buildDir, "go2.o")
			goarchive := filepath.Join(buildDir, "go.a")
			cgoarchive := ""

			gotool, err := testenv.GoTool()
			if err != nil {
				return err
			}

			go1src := filepath.Join("testdata", "go1.go")
			go2src := filepath.Join("testdata", "go2.go")

			importcfgfile := filepath.Join(buildDir, "importcfg")
			testenv.WriteImportcfg(t, importcfgfile, nil)

			out, err := exec.Command(gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go1obj, go1src).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
			}
			out, err = exec.Command(gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go2obj, go2src).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
			}
			out, err = exec.Command(gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
			}

			if testenv.HasCGO() {
				cgoarchive = filepath.Join(buildDir, "mycgo.a")
				gopath := filepath.Join(buildDir, "gopath")
				err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
				if err == nil {
					err = os.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
				}
				if err != nil {
					return err
				}
				cmd := exec.Command(gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
				cmd.Dir = filepath.Join(gopath, "src", "mycgo")
				cmd.Env = append(os.Environ(), "GOPATH="+gopath)
				out, err = cmd.CombinedOutput()
				if err != nil {
					return fmt.Errorf("go install mycgo: %v\n%s", err, out)
				}
			}

			builtGoobjs = goobjPaths{
				go1obj:     go1obj,
				go2obj:     go2obj,
				goarchive:  goarchive,
				cgoarchive: cgoarchive,
			}
			return nil
		}()<commit_msg>cmd/internal/archive: use testenv.Command instead of exec.Command in tests<commit_after>// Copyright 2017 The Go 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 archive

import (
	"bytes"
	"debug/elf"
	"debug/macho"
	"debug/pe"
	"fmt"
	"internal/testenv"
	"internal/xcoff"
	"io"
	"os"
	"path/filepath"
	"runtime"
	"sync"
	"testing"
	"unicode/utf8"
)

var buildDir string

func TestMain(m *testing.M) {
	if !testenv.HasGoBuild() {
		return
	}

	exit := m.Run()

	if buildDir != "" {
		os.RemoveAll(buildDir)
	}
	os.Exit(exit)
}

func copyDir(dst, src string) error {
	err := os.MkdirAll(dst, 0777)
	if err != nil {
		return err
	}
	entries, err := os.ReadDir(src)
	if err != nil {
		return err
	}
	for _, entry := range entries {
		err = copyFile(filepath.Join(dst, entry.Name()), filepath.Join(src, entry.Name()))
		if err != nil {
			return err
		}
	}
	return nil
}

func copyFile(dst, src string) (err error) {
	var s, d *os.File
	s, err = os.Open(src)
	if err != nil {
		return err
	}
	defer s.Close()
	d, err = os.Create(dst)
	if err != nil {
		return err
	}
	defer func() {
		e := d.Close()
		if err == nil {
			err = e
		}
	}()
	_, err = io.Copy(d, s)
	if err != nil {
		return err
	}
	return nil
}

var (
	buildOnce   sync.Once
	builtGoobjs goobjPaths
	buildErr    error
)

type goobjPaths struct {
	go1obj     string
	go2obj     string
	goarchive  string
	cgoarchive string
}

func buildGoobj(t *testing.T) goobjPaths {
	buildOnce.Do(func() {
		buildErr = func() (err error) {
			buildDir, err = os.MkdirTemp("", "TestGoobj")
			if err != nil {
				return err
			}

			go1obj := filepath.Join(buildDir, "go1.o")
			go2obj := filepath.Join(buildDir, "go2.o")
			goarchive := filepath.Join(buildDir, "go.a")
			cgoarchive := ""

			gotool, err := testenv.GoTool()
			if err != nil {
				return err
			}

			go1src := filepath.Join("testdata", "go1.go")
			go2src := filepath.Join("testdata", "go2.go")

			importcfgfile := filepath.Join(buildDir, "importcfg")
			testenv.WriteImportcfg(t, importcfgfile, nil)

			out, err := testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go1obj, go1src).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
			}
			out, err = testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go2obj, go2src).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
			}
			out, err = testenv.Command(t, gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
			if err != nil {
				return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
			}

			if testenv.HasCGO() {
				cgoarchive = filepath.Join(buildDir, "mycgo.a")
				gopath := filepath.Join(buildDir, "gopath")
				err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
				if err == nil {
					err = os.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
				}
				if err != nil {
					return err
				}
				cmd := testenv.Command(t, gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
				cmd.Dir = filepath.Join(gopath, "src", "mycgo")
				cmd.Env = append(os.Environ(), "GOPATH="+gopath)
				out, err = cmd.CombinedOutput()
				if err != nil {
					return fmt.Errorf("go install mycgo: %v\n%s", err, out)
				}
			}

			builtGoobjs = goobjPaths{
				go1obj:     go1obj,
				go2obj:     go2obj,
				goarchive:  goarchive,
				cgoarchive: cgoarchive,
			}
			return nil
		}()
{'fraction_non_alphanumeric': 0.16594711279007016, 'fraction_numerical': 0.010523475445223961, 'mean_word_length': 3.1952461799660443, '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': '7046114', 'n_tokens_mistral': 3257, 'n_tokens_neox': 2912, 'n_words': 724}