code-intelligence.com/cifuzz@v0.40.0/internal/bundler/bundler_test.go (about)

     1  package bundler
     2  
     3  import (
     4  	"path/filepath"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/spf13/viper"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"code-intelligence.com/cifuzz/internal/testutil"
    13  )
    14  
    15  func TestParsingAdditionalFilesArguments(t *testing.T) {
    16  	viper.Set("verbose", true)
    17  
    18  	type test struct {
    19  		input      string
    20  		wantSource string
    21  		wantTarget string
    22  	}
    23  	tests := map[string]test{
    24  		"one_file": {
    25  			input:      "file.so",
    26  			wantSource: "file.so",
    27  			wantTarget: filepath.Join("work_dir", "file.so")},
    28  		"one_file_in_dir": {
    29  			input:      filepath.Join("source", "path", "file.so"),
    30  			wantSource: filepath.Join("source", "path", "file.so"),
    31  			wantTarget: filepath.Join("work_dir", "file.so")},
    32  		"rename": {
    33  			input:      filepath.Join("path", "file.so") + ";" + filepath.Join("path", "new.so"),
    34  			wantSource: filepath.Join("path", "file.so"),
    35  			wantTarget: filepath.Join("path", "new.so"),
    36  		},
    37  		"dir": {
    38  			input:      "path",
    39  			wantSource: "path",
    40  			wantTarget: filepath.Join("work_dir", "path"),
    41  		},
    42  		"sub dir": {
    43  			input:      filepath.Join("path", "source"),
    44  			wantSource: filepath.Join("path", "source"),
    45  			wantTarget: filepath.Join("work_dir", "source"),
    46  		},
    47  		"rename_dir": {
    48  			input:      "source;target",
    49  			wantSource: "source",
    50  			wantTarget: "target",
    51  		},
    52  	}
    53  
    54  	// add special windows test cases for absolute windows paths
    55  	if runtime.GOOS == "windows" {
    56  		tests["win_absolute_1"] = test{
    57  			input:      filepath.Join("C:", "foo", "file.so"),
    58  			wantSource: filepath.Join("C:", "foo", "file.so"),
    59  			wantTarget: filepath.Join("work_dir", "file.so"),
    60  		}
    61  		tests["win_absolute_2"] = test{
    62  			input:      filepath.Join("C:", "foo", "file.so") + ";new.so",
    63  			wantSource: filepath.Join("C:", "foo", "file.so"),
    64  			wantTarget: "new.so",
    65  		}
    66  	}
    67  
    68  	for name, tc := range tests {
    69  		t.Run(name, func(t *testing.T) {
    70  			source, target, err := parseAdditionalFilesArgument(tc.input)
    71  			require.NoError(t, err)
    72  			assert.Equal(t, tc.wantSource, source)
    73  			assert.Equal(t, tc.wantTarget, target)
    74  		})
    75  	}
    76  }
    77  
    78  // If an error occurs during bundling there should be no
    79  // broken bundle file left
    80  func TestRemoveBundleOnError(t *testing.T) {
    81  	testDir := testutil.MkdirTemp(t, "", "bundle-delete-*")
    82  	bundlePath := filepath.Join(testDir, "artifact.tar.gz")
    83  	opts := &Opts{
    84  		// using invalid build system to make the bundling fail
    85  		BuildSystem: "FOO",
    86  		OutputPath:  bundlePath,
    87  	}
    88  	bundler := New(opts)
    89  
    90  	path, err := bundler.Bundle()
    91  	require.Empty(t, path)
    92  	require.Error(t, err)
    93  
    94  	assert.NoFileExists(t, bundlePath)
    95  }