github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/whitesource/configHelper_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package whitesource
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRewriteUAConfigurationFile(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	t.Run("default", func(t *testing.T) {
    17  		config := ScanOptions{
    18  			BuildTool:      "npm",
    19  			ConfigFilePath: "ua.props",
    20  		}
    21  		utilsMock := NewScanUtilsMock()
    22  		utilsMock.AddFile(config.ConfigFilePath, []byte("test = dummy"))
    23  
    24  		path, err := config.RewriteUAConfigurationFile(utilsMock, "")
    25  		assert.NoError(t, err)
    26  		newUAConfig, err := utilsMock.FileRead(path)
    27  		assert.NoError(t, err)
    28  		assert.Contains(t, string(newUAConfig), "test = dummy")
    29  		assert.Contains(t, string(newUAConfig), "failErrorLevel = ALL")
    30  	})
    31  
    32  	t.Run("accept non-existing file", func(t *testing.T) {
    33  		config := ScanOptions{
    34  			BuildTool:      "npm",
    35  			ConfigFilePath: "ua.props",
    36  		}
    37  		utilsMock := NewScanUtilsMock()
    38  
    39  		path, err := config.RewriteUAConfigurationFile(utilsMock, "")
    40  		assert.NoError(t, err)
    41  
    42  		newUAConfig, err := utilsMock.FileRead(path)
    43  		assert.NoError(t, err)
    44  		assert.Contains(t, string(newUAConfig), "failErrorLevel = ALL")
    45  	})
    46  
    47  	t.Run("error - write file", func(t *testing.T) {
    48  		config := ScanOptions{
    49  			BuildTool:      "npm",
    50  			ConfigFilePath: "ua.props",
    51  		}
    52  		utilsMock := NewScanUtilsMock()
    53  		utilsMock.FileWriteError = fmt.Errorf("failed to write file")
    54  
    55  		_, err := config.RewriteUAConfigurationFile(utilsMock, "")
    56  		assert.Contains(t, fmt.Sprint(err), "failed to write file")
    57  	})
    58  }
    59  
    60  func TestUpdateConfig(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	originalConfig := map[string]string{
    64  		"dependent":            "dependentValue",
    65  		"forced":               "forced_original",
    66  		"not_forced":           "not_forced_original",
    67  		"dont_omit_forced":     "dont_omit_forced_original",
    68  		"dont_omit_not_forced": "dont_omit_not_forced_original",
    69  		"append":               "original_value appended by",
    70  		"append_empty":         "",
    71  	}
    72  	testConfig := ConfigOptions{
    73  		{Name: "non_existing_forced", Value: "non_existing_forced_val", Force: true},
    74  		{Name: "non_existing_not_forced", Value: "non_existing_not_forced_val", Force: false},
    75  		{Name: "forced", Value: "forced_val", Force: true},
    76  		{Name: "not_forced", Value: "not_forced_val", Force: false},
    77  		{Name: "omit", Value: "omit_val", OmitIfPresent: "dependent"},
    78  		{Name: "dont_omit", Value: "dont_omit_val", OmitIfPresent: "dependent_notExisting"},
    79  		{Name: "dont_omit_forced", Value: "dont_omit_forced_val", OmitIfPresent: "dependent_notExisting", Force: true},
    80  		{Name: "dont_omit_not_forced", Value: "dont_omit_not_forced_val", OmitIfPresent: "dependent_notExisting", Force: false},
    81  		{Name: "append", Value: "appended_val", Append: true},
    82  		{Name: "append_empty", Value: "appended_val", Append: true},
    83  	}
    84  
    85  	updatedConfig := testConfig.updateConfig(&originalConfig)
    86  
    87  	assert.Equal(t, "dependentValue", updatedConfig["dependent"])
    88  	assert.Equal(t, "non_existing_forced_val", updatedConfig["non_existing_forced"])
    89  	assert.Equal(t, "non_existing_not_forced_val", updatedConfig["non_existing_not_forced"])
    90  	assert.Equal(t, "forced_val", updatedConfig["forced"])
    91  	assert.Equal(t, "not_forced_original", updatedConfig["not_forced"])
    92  	assert.NotEqual(t, "omit_val", updatedConfig["omit"])
    93  	assert.Equal(t, "dont_omit_val", updatedConfig["dont_omit"])
    94  	assert.Equal(t, "dont_omit_forced_val", updatedConfig["dont_omit_forced"])
    95  	assert.Equal(t, "dont_omit_not_forced_original", updatedConfig["dont_omit_not_forced"])
    96  	assert.Equal(t, "original_value appended by appended_val", updatedConfig["append"])
    97  	assert.Equal(t, "appended_val", updatedConfig["append_empty"])
    98  }
    99  
   100  func TestAddGeneralDefaults(t *testing.T) {
   101  	t.Parallel()
   102  
   103  	utilsMock := NewScanUtilsMock()
   104  
   105  	t.Run("default", func(t *testing.T) {
   106  		testConfig := ConfigOptions{}
   107  		whitesourceConfig := ScanOptions{
   108  			OrgToken:       "testOrgToken",
   109  			ProductName:    "Test",
   110  			ProductToken:   "testProductToken",
   111  			ProductVersion: "testVersion",
   112  			ProjectName:    "testProject",
   113  			UserToken:      "testuserKey",
   114  		}
   115  		testConfig.addGeneralDefaults(&whitesourceConfig, utilsMock, "")
   116  		assert.Equal(t, "checkPolicies", testConfig[0].Name)
   117  		assert.Equal(t, true, testConfig[0].Value)
   118  		assert.Equal(t, "forceCheckAllDependencies", testConfig[1].Name)
   119  		assert.Equal(t, true, testConfig[1].Value)
   120  
   121  		assert.Equal(t, "testOrgToken", testConfig[2].Value)
   122  		assert.Equal(t, "Test", testConfig[3].Value)
   123  		assert.Equal(t, "testVersion", testConfig[4].Value)
   124  		assert.Equal(t, "testProject", testConfig[5].Value)
   125  		assert.Equal(t, "testVersion", testConfig[6].Value)
   126  		assert.Equal(t, "testProductToken", testConfig[7].Value)
   127  		assert.Equal(t, "testuserKey", testConfig[8].Value)
   128  	})
   129  
   130  	t.Run("DIST product", func(t *testing.T) {
   131  		testConfig := ConfigOptions{}
   132  		whitesourceConfig := ScanOptions{
   133  			OrgToken:       "testOrgToken",
   134  			ProductName:    "DIST - Test",
   135  			ProductToken:   "testProductToken",
   136  			ProductVersion: "testVersion",
   137  			ProjectName:    "testProject",
   138  			UserToken:      "testuserKey",
   139  		}
   140  		testConfig.addGeneralDefaults(&whitesourceConfig, utilsMock, "anotherProject")
   141  		assert.Equal(t, "checkPolicies", testConfig[0].Name)
   142  		assert.Equal(t, false, testConfig[0].Value)
   143  		assert.Equal(t, "forceCheckAllDependencies", testConfig[1].Name)
   144  		assert.Equal(t, false, testConfig[1].Value)
   145  		assert.Equal(t, "anotherProject", testConfig[5].Value)
   146  	})
   147  
   148  	t.Run("verbose", func(t *testing.T) {
   149  		testConfig := ConfigOptions{}
   150  		whitesourceConfig := ScanOptions{
   151  			Verbose: true,
   152  		}
   153  		testConfig.addGeneralDefaults(&whitesourceConfig, utilsMock, "")
   154  		assert.Equal(t, "log.level", testConfig[2].Name)
   155  		assert.Equal(t, "debug", testConfig[2].Value)
   156  		assert.Equal(t, "log.files.level", testConfig[3].Name)
   157  		assert.Equal(t, "debug", testConfig[3].Value)
   158  	})
   159  
   160  	t.Run("includes and excludes", func(t *testing.T) {
   161  		testConfig := ConfigOptions{}
   162  		whitesourceConfig := ScanOptions{
   163  			Excludes: []string{"**/excludes1", "**/excludes2"},
   164  			Includes: []string{"**/includes1", "**/includes2"},
   165  		}
   166  		testConfig.addGeneralDefaults(&whitesourceConfig, utilsMock, "")
   167  		assert.Equal(t, "excludes", testConfig[2].Name)
   168  		assert.Equal(t, "**/excludes1 **/excludes2", testConfig[2].Value)
   169  		assert.Equal(t, true, testConfig[2].Force)
   170  		assert.Equal(t, "includes", testConfig[3].Name)
   171  		assert.Equal(t, "**/includes1 **/includes2", testConfig[3].Value)
   172  		assert.Equal(t, true, testConfig[3].Force)
   173  	})
   174  }
   175  
   176  func TestAddBuildToolDefaults(t *testing.T) {
   177  	t.Parallel()
   178  
   179  	t.Run("success case", func(t *testing.T) {
   180  		utilsMock := NewScanUtilsMock()
   181  		var testConfig ConfigOptions
   182  		whitesourceConfig := ScanOptions{
   183  			BuildTool: "dub",
   184  		}
   185  		err := testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   186  		assert.NoError(t, err)
   187  		assert.Equal(t, ConfigOptions{{Name: "ignoreSourceFiles", Value: true, Force: true}, {Name: "includes", Value: "**/*.d **/*.di"}}, testConfig)
   188  	})
   189  
   190  	t.Run("success case", func(t *testing.T) {
   191  		utilsMock := NewScanUtilsMock()
   192  		var testConfig ConfigOptions
   193  		whitesourceConfig := ScanOptions{
   194  			BuildTool: "dub2",
   195  		}
   196  		err := testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   197  		assert.NoError(t, err)
   198  		assert.Equal(t, ConfigOptions{{Name: "fileSystemScan", Value: false, Force: true}, {Name: "includes", Value: "**/*.d **/*.di"}}, testConfig)
   199  	})
   200  
   201  	t.Run("error case", func(t *testing.T) {
   202  		utilsMock := NewScanUtilsMock()
   203  		var testConfig ConfigOptions
   204  		whitesourceConfig := ScanOptions{
   205  			BuildTool: "notHardened",
   206  		}
   207  		err := testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   208  		assert.EqualError(t, err, "configuration not hardened")
   209  	})
   210  
   211  	t.Run("maven - m2 path", func(t *testing.T) {
   212  		utilsMock := NewScanUtilsMock()
   213  		testConfig := ConfigOptions{}
   214  		whitesourceConfig := ScanOptions{
   215  			BuildTool: "maven",
   216  			M2Path:    "test/.m2",
   217  		}
   218  		testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   219  		assert.Contains(t, testConfig, ConfigOption{Name: "maven.m2RepositoryPath", Value: "test/.m2", Force: true})
   220  		assert.NotContains(t, testConfig, ConfigOption{Name: "maven.additionalArguments", Value: "", Force: true})
   221  	})
   222  
   223  	t.Run("maven - settings", func(t *testing.T) {
   224  		utilsMock := NewScanUtilsMock()
   225  		testConfig := ConfigOptions{}
   226  		whitesourceConfig := ScanOptions{
   227  			BuildTool:                  "maven",
   228  			ProjectSettingsFile:        "project-settings.xml",
   229  			GlobalSettingsFile:         "global-settings.xml",
   230  			BuildDescriptorExcludeList: []string{"unit-tests/pom.xml"},
   231  		}
   232  		utilsMock.AddFile("unit-tests/pom.xml", []byte("dummy"))
   233  		testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   234  		assert.Contains(t, testConfig, ConfigOption{Name: "maven.additionalArguments", Value: "--global-settings global-settings.xml --settings project-settings.xml --projects !unit-tests", Append: true})
   235  	})
   236  
   237  	t.Run("Docker - default", func(t *testing.T) {
   238  		utilsMock := NewScanUtilsMock()
   239  		testConfig := ConfigOptions{}
   240  		whitesourceConfig := ScanOptions{
   241  			BuildTool: "docker",
   242  		}
   243  		utilsMock.AddFile("Dockerfile", []byte("dummy"))
   244  		testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   245  		assert.Contains(t, testConfig, ConfigOption{Name: "docker.dockerfilePath", Value: "Dockerfile", Force: false})
   246  	})
   247  
   248  	t.Run("Docker - no builddescriptor found", func(t *testing.T) {
   249  		utilsMock := NewScanUtilsMock()
   250  		testConfig := ConfigOptions{}
   251  		whitesourceConfig := ScanOptions{
   252  			BuildTool: "docker",
   253  		}
   254  		testConfig.addBuildToolDefaults(&whitesourceConfig, utilsMock)
   255  		assert.NotContains(t, testConfig, ConfigOption{Name: "docker.dockerfilePath", Value: "Dockerfile", Force: false})
   256  	})
   257  }
   258  
   259  func TestMvnProjectExcludes(t *testing.T) {
   260  	utilsMock := NewScanUtilsMock()
   261  	utilsMock.AddFile("unit-tests/package.json", []byte("dummy"))
   262  	utilsMock.AddFile("unit-tests/pom.xml", []byte("dummy"))
   263  	utilsMock.AddFile("integration-tests/pom.xml", []byte("dummy"))
   264  	tt := []struct {
   265  		buildDescriptorExcludeList []string
   266  		expected                   []string
   267  	}{
   268  		{buildDescriptorExcludeList: []string{}, expected: []string{}},
   269  		{buildDescriptorExcludeList: []string{"unit-tests/package.json", "integration-tests/package.json"}, expected: []string{}},
   270  		{buildDescriptorExcludeList: []string{"unit-tests/pom.xml"}, expected: []string{"--projects", "!unit-tests"}},
   271  		{buildDescriptorExcludeList: []string{"unit-tests/pom.xml", "integration-tests/pom.xml"}, expected: []string{"--projects", "!unit-tests,!integration-tests"}},
   272  	}
   273  
   274  	for _, test := range tt {
   275  		assert.Equal(t, test.expected, mvnProjectExcludes(test.buildDescriptorExcludeList, utilsMock), test.buildDescriptorExcludeList)
   276  	}
   277  }