code-intelligence.com/cifuzz@v0.40.0/internal/cmd/bundle/bundle_test.go (about)

     1  package bundle
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/spf13/viper"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"code-intelligence.com/cifuzz/internal/bundler"
    16  	"code-intelligence.com/cifuzz/internal/cmdutils"
    17  	"code-intelligence.com/cifuzz/internal/config"
    18  	"code-intelligence.com/cifuzz/internal/testutil"
    19  	"code-intelligence.com/cifuzz/pkg/dependencies"
    20  	"code-intelligence.com/cifuzz/pkg/log"
    21  	"code-intelligence.com/cifuzz/util/fileutil"
    22  )
    23  
    24  var testOut io.ReadWriter
    25  
    26  func TestMain(m *testing.M) {
    27  	// capture log output
    28  	testOut = bytes.NewBuffer([]byte{})
    29  	oldOut := log.Output
    30  	log.Output = testOut
    31  	viper.Set("verbose", true)
    32  
    33  	// Make the bundle command not fail on unsupported platforms to be
    34  	// able to test it on all platforms, reset the env variable after the test.
    35  	allowUnsupportedPlatformsEnv := os.Getenv(config.AllowUnsupportedPlatformsEnv)
    36  	defer func() {
    37  		err := os.Setenv(config.AllowUnsupportedPlatformsEnv, allowUnsupportedPlatformsEnv)
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  	}()
    42  	err := os.Setenv(config.AllowUnsupportedPlatformsEnv, "1")
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  
    47  	m.Run()
    48  
    49  	log.Output = oldOut
    50  }
    51  
    52  func TestUnknownBuildSystem(t *testing.T) {
    53  	_, err := cmdutils.ExecuteCommand(t, New(), os.Stdin)
    54  	require.Error(t, err)
    55  
    56  	// In this scenario a log with the error message will be created and we do not care about it here
    57  	fileutil.Cleanup(".cifuzz-build")
    58  }
    59  
    60  func TestClangMissing(t *testing.T) {
    61  	dependencies.TestMockAllDeps(t)
    62  	dependencies.OverwriteUninstalled(dependencies.GetDep(dependencies.Clang))
    63  
    64  	opts := &options{}
    65  	opts.BuildSystem = config.BuildSystemCMake
    66  
    67  	// clone the example project because this command needs to parse an actual
    68  	// project config... if there is none it will fail before the dependency check
    69  	_, cleanup := testutil.BootstrapExampleProjectForTest("run-cmd-test", config.BuildSystemCMake)
    70  	defer cleanup()
    71  
    72  	_, err := cmdutils.ExecuteCommand(t, newWithOptions(opts), os.Stdin)
    73  	require.Error(t, err)
    74  
    75  	output, err := io.ReadAll(testOut)
    76  	require.NoError(t, err)
    77  	assert.Contains(t, string(output), fmt.Sprintf(dependencies.MessageMissing, "clang"))
    78  }
    79  
    80  func TestClangVersion(t *testing.T) {
    81  	dependencies.TestMockAllDeps(t)
    82  
    83  	dep := dependencies.GetDep(dependencies.Clang)
    84  	version := dependencies.OverwriteGetVersionWith0(dep)
    85  
    86  	opts := &options{}
    87  	opts.BuildSystem = config.BuildSystemCMake
    88  
    89  	// clone the example project because this command needs to parse an actual
    90  	// project config... if there is none it will fail before the dependency check
    91  	_, cleanup := testutil.BootstrapExampleProjectForTest("run-cmd-test", config.BuildSystemCMake)
    92  	defer cleanup()
    93  
    94  	_, err := cmdutils.ExecuteCommand(t, newWithOptions(opts), os.Stdin)
    95  	require.Error(t, err)
    96  
    97  	output, err := io.ReadAll(testOut)
    98  	require.NoError(t, err)
    99  	assert.Contains(t, string(output),
   100  		fmt.Sprintf(dependencies.MessageVersion, "clang", dep.MinVersion.String(), version))
   101  }
   102  
   103  func TestCMakeMissing(t *testing.T) {
   104  	dependencies.TestMockAllDeps(t)
   105  	dependencies.OverwriteUninstalled(dependencies.GetDep(dependencies.CMake))
   106  
   107  	opts := &options{}
   108  	opts.BuildSystem = config.BuildSystemCMake
   109  
   110  	// clone the example project because this command needs to parse an actual
   111  	// project config... if there is none it will fail before the dependency check
   112  	_, cleanup := testutil.BootstrapExampleProjectForTest("run-cmd-test", config.BuildSystemCMake)
   113  	defer cleanup()
   114  
   115  	_, err := cmdutils.ExecuteCommand(t, newWithOptions(opts), os.Stdin)
   116  	require.Error(t, err)
   117  
   118  	output, err := io.ReadAll(testOut)
   119  	require.NoError(t, err)
   120  
   121  	assert.Contains(t, string(output), fmt.Sprintf(dependencies.MessageMissing, "cmake"))
   122  }
   123  
   124  func TestEnvVarsSetInConfigFile(t *testing.T) {
   125  	projectDir := testutil.BootstrapEmptyProject(t, "bundle-test-")
   126  	t.Cleanup(func() { fileutil.Cleanup(projectDir) })
   127  	configFileContent := `env:
   128    - FOO=foo
   129    - BAR
   130    - NO_SUCH_VARIABLE
   131  `
   132  	err := os.WriteFile(filepath.Join(projectDir, "cifuzz.yaml"), []byte(configFileContent), 0644)
   133  	require.NoError(t, err)
   134  
   135  	t.Setenv("BAR", "bar")
   136  
   137  	opts := &options{bundler.Opts{
   138  		ProjectDir:  projectDir,
   139  		ConfigDir:   projectDir,
   140  		BuildSystem: config.BuildSystemCMake,
   141  	}}
   142  
   143  	cmd := newWithOptions(opts)
   144  	err = cmd.PreRunE(cmd, nil)
   145  	require.NoError(t, err)
   146  
   147  	require.Equal(t, []string{"FOO=foo", "BAR=bar"}, opts.Env)
   148  }