github.com/verrazzano/verrazzano@v1.7.0/tools/copyright/copyright_test.go (about)

     1  // Copyright (c) 2021, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package main
     4  
     5  import (
     6  	"github.com/stretchr/testify/assert"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  // TestRunScan Tests the main scan driver function
    12  // GIVEN a call to runScan
    13  // WHEN with a directory to scan
    14  // THEN the scan results are as expected
    15  func TestRunScan(t *testing.T) {
    16  	verbose = true
    17  	ec := runScan([]string{"test"})
    18  	assert.Equal(t, 1, ec)
    19  	assert.Equal(t, uint(6), numFilesAnalyzed)
    20  	assert.Equal(t, 5, len(filesWithErrors))
    21  	assert.Equal(t, uint(1), numFilesSkipped)
    22  	assert.Equal(t, uint(1), numDirectoriesSkipped)
    23  }
    24  
    25  // TestRunScanNoArgs Tests the main scan driver function
    26  // GIVEN a call to runScan
    27  // WHEN no args are provided
    28  // THEN a non-zero code is returned
    29  func TestRunScanNoArgs(t *testing.T) {
    30  	assert.Equal(t, 1, runScan([]string{}))
    31  }
    32  
    33  // TestRunScanNoArgs Tests the main scan driver function
    34  // GIVEN a call to runScan
    35  // WHEN a non-existent path is provided
    36  // THEN a zero error code is returned (the path is ignored)
    37  func TestRunScanPathDoesNotExist(t *testing.T) {
    38  	assert.Equal(t, 0, runScan([]string{"foo"}))
    39  }
    40  
    41  // TestContains test the contains() utility fn
    42  // GIVEN a list of strings
    43  // WHEN contains is called with valid and invalid strings
    44  // THEN true is returned when the item is present, false otherwise
    45  func TestContains(t *testing.T) {
    46  	assert.True(t, contains([]string{"foo", "bar", "thud"}, "foo"))
    47  	assert.True(t, contains([]string{"foo", "bar", "thud"}, "bar"))
    48  	assert.True(t, contains([]string{"foo", "bar", "thud"}, "thud"))
    49  	assert.False(t, contains([]string{"foo", "bar", "thud"}, "thwack"))
    50  	assert.False(t, contains([]string{}, "foo"))
    51  	assert.False(t, contains([]string{"foo", "bar", "thud"}, ""))
    52  }
    53  
    54  // TestPrintScanReport Keep code coverage happy if we need to
    55  func TestPrintScanReport(t *testing.T) {
    56  	filesWithErrors = make(map[string][]string, 10)
    57  	filesWithErrors["test1"] = []string{"Some kind of error"}
    58  	printScanReport()
    59  	printUsage()
    60  }
    61  
    62  // TestSkipOrIgnoreDir tests the skip/ignore dir logic
    63  // GIVEN a call to skipOrIgnoreDir
    64  // WHEN paths are passed that do and do not match the skip criteria
    65  // THEN true is returned if the dir is to be skipped, false otherwise
    66  func TestSkipOrIgnoreDir(t *testing.T) {
    67  
    68  	saveIgnores := directoriesToIgnore
    69  	defer func() {
    70  		directoriesToIgnore = saveIgnores
    71  	}()
    72  
    73  	directoriesToIgnore = append(directoriesToIgnore, "someDirToIgnore")
    74  	skippedCountBefore := numDirectoriesSkipped
    75  	assert.True(t, skipOrIgnoreDir(directoriesToSkip[0], "foo"))
    76  	assert.True(t, skipOrIgnoreDir("foo", directoriesToIgnore[0]))
    77  	assert.False(t, skipOrIgnoreDir("foo", "bar"))
    78  	assert.Equal(t, skippedCountBefore+2, numDirectoriesSkipped)
    79  }
    80  
    81  // TestSkipOrIgnoreDir tests the skip/ignore dir logic
    82  // GIVEN a call to skipOrIgnoreDir
    83  // WHEN paths are passed that do and do not match the skip criteria
    84  // THEN true is returned if the dir is to be skipped, false otherwise
    85  func TestLoadIgnoreFile(t *testing.T) {
    86  
    87  	defer os.Unsetenv("COPYRIGHT_INGOREFILE_PATH")
    88  
    89  	os.Setenv("COPYRIGHT_INGOREFILE_PATH", "./thud.txt")
    90  	err := loadIgnoreFile()
    91  	assert.NotNil(t, err)
    92  
    93  	os.Setenv("COPYRIGHT_INGOREFILE_PATH", "./"+ignoreFileDefaultName)
    94  	err = loadIgnoreFile()
    95  	assert.Nil(t, err)
    96  	assert.True(t, len(directoriesToIgnore) > 0)
    97  	assert.True(t, len(filesToIgnore) > 0)
    98  }
    99  
   100  // TestCheckFileNoLicense tests checkFile with a file with no license string
   101  // GIVEN a call to checkFile
   102  // WHEN with a file with valid license and copyright info
   103  // THEN No errors are reported
   104  func TestCheckFile(t *testing.T) {
   105  	runCheckFileTest(t, "test/include/good.txt", false)
   106  }
   107  
   108  // TestCheckFileNoLicense tests checkFile with a file with no license string
   109  // GIVEN a call to checkFile
   110  // WHEN with a file with no license string
   111  // THEN Errors are reported
   112  func TestCheckFileNoLicense(t *testing.T) {
   113  	runCheckFileTest(t, "test/include/nolicense.txt", true)
   114  }
   115  
   116  // TestCheckFileNoCopyright tests checkFile with a file with no license string
   117  // GIVEN a call to checkFile
   118  // WHEN with a file with no copyright string
   119  // THEN Errors are reported
   120  func TestCheckFileNoCopyright(t *testing.T) {
   121  	runCheckFileTest(t, "test/include/nocopyright.txt", true)
   122  }
   123  
   124  // TestCheckFileBadCopyright tests checkFile with a file
   125  // GIVEN a call to checkFile
   126  // WHEN with a file with a bad copyright string
   127  // THEN Errors are reported
   128  func TestCheckFileBadCopyright(t *testing.T) {
   129  	runCheckFileTest(t, "test/include/badcopyright.txt", true)
   130  }
   131  
   132  // TestCheckFileBadLicense tests checkFile with a file
   133  // GIVEN a call to checkFile
   134  // WHEN with a file with a bad license string
   135  // THEN Errors are reported
   136  func TestCheckFileBadLicense(t *testing.T) {
   137  	runCheckFileTest(t, "test/include/badlicense.txt", true)
   138  }
   139  
   140  // TestCheckFileBuriedInfo tests checkFile with a file
   141  // GIVEN a call to checkFile
   142  // WHEN with a file with valid license/copyright info buried too far down (over 5 lines)
   143  // THEN Errors are reported
   144  func TestCheckFileBuriedInfo(t *testing.T) {
   145  	runCheckFileTest(t, "test/include/buried.txt", true)
   146  }
   147  
   148  // TestCheckFileFileOnIgnoreList tests checkFile with a file
   149  // GIVEN a call to checkFile
   150  // WHEN with a file on the ignore list
   151  // THEN No errors are reported and the skip count increments
   152  func TestCheckFileFileOnIgnoreList(t *testing.T) {
   153  	loadIgnoreFile()
   154  	defer func() {
   155  		filesToIgnore = []string{}
   156  		directoriesToIgnore = []string{}
   157  	}()
   158  	beforeTestSkippedCount := numFilesSkipped
   159  	runCheckFileTest(t, "test/include/ignore.txt", false)
   160  	assert.Equal(t, beforeTestSkippedCount+1, numFilesSkipped)
   161  }
   162  
   163  func runCheckFileTest(t *testing.T, fileName string, expectErrors bool) {
   164  	filesWithErrors = make(map[string][]string, 10)
   165  	verbose = true
   166  
   167  	numErrors := 0
   168  	if expectErrors {
   169  		numErrors = 1
   170  	}
   171  
   172  	info, err := os.Stat(fileName)
   173  	assert.Nil(t, err)
   174  	assert.Nil(t, checkFile(fileName, info))
   175  	assert.True(t, len(filesWithErrors) == numErrors)
   176  	_, ok := filesWithErrors[fileName]
   177  	assert.Equal(t, expectErrors, ok)
   178  	printScanReport()
   179  }