<commit_before>/*
Copyright 2018 The CDI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"testing"

	"github.com/pkg/errors"
)

type testProcessLimiter struct {
	addressSpaceError error
	cpuTimeError      error
}

func newTestProcessLimiter(addressSpaceError, cpuTimeError error) ProcessLimiter {
	return &testProcessLimiter{addressSpaceError, cpuTimeError}
}

func (p *testProcessLimiter) SetAddressSpaceLimit(pid int, value uint64) error {
	return p.addressSpaceError
}

func (p *testProcessLimiter) SetCPUTimeLimit(pid int, value uint64) error {
	return p.cpuTimeError
}

func fakeCommand(command string, args ...string) *exec.Cmd {
	cs := []string{"-test.run=TestHelperProcess", "--", command}
	cs = append(cs, args...)

	cmd := exec.Command(os.Args[0], cs...)
	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
	return cmd
}

func badCommand(string, ...string) *exec.Cmd {
	return exec.Command("/usr/bin/doesnotexist")
}

func TestExec(t *testing.T) {
	type args struct {
		commandOverride func(string, ...string) *exec.Cmd
		limiter         ProcessLimiter
		limits          *ProcessLimitValues
		command         string
		args            []string
	}

	limits := &ProcessLimitValues{1, 1}
	nullLimiter := newTestProcessLimiter(nil, nil)

	tests := []struct {
		name      string
		args      args
		output    string
		errString string
	}{
		{
			"command success no limits",
			args{fakeCommand, nil, nil, "faker", []string{"0", "", ""}},
			"",
			"",
		},
		{
			"command start fails",
			args{badCommand, nullLimiter, limits, "faker", []string{"", "", ""}},
			"",
			"fork/exec /usr/bin/doesnotexist: no such file or directory",
		},
		{
			"address space limit fails",
			args{fakeCommand, newTestProcessLimiter(errors.New("Set address limit fails"), nil), limits, "faker", []string{"", "", ""}},
			"",
			"Set address limit fails",
		},
		{
			"address space limit fails",
			args{fakeCommand, newTestProcessLimiter(nil, errors.New("Set CPU limit fails")), limits, "faker", []string{"", "", ""}},
			"",
			"Set CPU limit fails",
		},
		{
			"command exit bad",
			args{fakeCommand, nullLimiter, limits, "faker", []string{"1", "", ""}},
			"",
			"exit status 1",
		},<commit_msg>update tests for better coverage<commit_after>/*
Copyright 2018 The CDI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"testing"

	"github.com/pkg/errors"
)

type testProcessLimiter struct {
	addressSpaceError error
	cpuTimeError      error
}

func newTestProcessLimiter(addressSpaceError, cpuTimeError error) ProcessLimiter {
	return &testProcessLimiter{addressSpaceError, cpuTimeError}
}

func (p *testProcessLimiter) SetAddressSpaceLimit(pid int, value uint64) error {
	return p.addressSpaceError
}

func (p *testProcessLimiter) SetCPUTimeLimit(pid int, value uint64) error {
	return p.cpuTimeError
}

func fakeCommand(command string, args ...string) *exec.Cmd {
	cs := []string{"-test.run=TestHelperProcess", "--", command}
	cs = append(cs, args...)

	cmd := exec.Command(os.Args[0], cs...)
	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
	return cmd
}

func badCommand(string, ...string) *exec.Cmd {
	return exec.Command("/usr/bin/doesnotexist")
}

func TestExec(t *testing.T) {
	type args struct {
		commandOverride func(string, ...string) *exec.Cmd
		limiter         ProcessLimiter
		limits          *ProcessLimitValues
		command         string
		args            []string
	}

	limits := &ProcessLimitValues{1, 1}
	nullLimiter := newTestProcessLimiter(nil, nil)

	tests := []struct {
		name      string
		args      args
		output    string
		errString string
	}{
		{
			"command success with real limits",
			args{fakeCommand, nil, &ProcessLimitValues{1 << 30, 10}, "faker", []string{"0", "", ""}},
			"",
			"",
		},
		{
			"command start fails",
			args{badCommand, nullLimiter, limits, "faker", []string{"", "", ""}},
			"",
			"fork/exec /usr/bin/doesnotexist: no such file or directory",
		},
		{
			"address space limit fails",
			args{fakeCommand, newTestProcessLimiter(errors.New("Set address limit fails"), nil), limits, "faker", []string{"", "", ""}},
			"",
			"Set address limit fails",
		},
		{
			"address space limit fails",
			args{fakeCommand, newTestProcessLimiter(nil, errors.New("Set CPU limit fails")), limits, "faker", []string{"", "", ""}},
			"",
			"Set CPU limit fails",
		},
		{
			"command exit bad",
			args{fakeCommand, nullLimiter, limits, "faker", []string{"1", "", ""}},
			"",
			"exit status 1",
		},
{'fraction_non_alphanumeric': 0.13647058823529412, 'fraction_numerical': 0.007782805429864253, 'mean_word_length': 3.885941644562334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1844760', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1726, 'n_words': 519}