github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/ignore/ignore_test.go (about)

     1  package ignore
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/kubesphere/s2irun/pkg/api"
    10  	"github.com/kubesphere/s2irun/pkg/api/constants"
    11  	"github.com/kubesphere/s2irun/pkg/utils/fs"
    12  )
    13  
    14  func baseTest(t *testing.T, patterns []string, filesToDel []string, filesToKeep []string) {
    15  	// create working dir
    16  	workingDir, werr := fs.NewFileSystem().CreateWorkingDirectory()
    17  	if werr != nil {
    18  		t.Errorf("problem allocating working dir: %v", werr)
    19  	} else {
    20  		t.Logf("working directory is %q", workingDir)
    21  	}
    22  	defer func() {
    23  		// clean up test
    24  		cleanerr := os.RemoveAll(workingDir)
    25  		if cleanerr != nil {
    26  			t.Errorf("problem cleaning up: %v", cleanerr)
    27  		}
    28  	}()
    29  
    30  	c := &api.Config{WorkingDir: workingDir}
    31  
    32  	// create source repo dir for .s2iignore that matches where ignore.go looks
    33  	dpath := filepath.Join(c.WorkingDir, "upload", "src")
    34  	derr := os.MkdirAll(dpath, 0777)
    35  	if derr != nil {
    36  		t.Errorf("Problem creating source repo dir %q: %v", dpath, derr)
    37  	}
    38  
    39  	c.WorkingSourceDir = dpath
    40  	t.Logf("working source dir %q", dpath)
    41  
    42  	// create s2iignore file
    43  	ipath := filepath.Join(dpath, constants.IgnoreFile)
    44  	ifile, ierr := os.Create(ipath)
    45  	defer ifile.Close()
    46  	if ierr != nil {
    47  		t.Errorf("Problem creating .s2iignore in %q: %v", ipath, ierr)
    48  	}
    49  
    50  	// write patterns to remove into s2ignore, but save ! exclusions
    51  	filesToIgnore := make(map[string]string)
    52  	for _, pattern := range patterns {
    53  		t.Logf("storing pattern %q", pattern)
    54  		_, serr := ifile.WriteString(pattern)
    55  
    56  		if serr != nil {
    57  			t.Errorf("Problem setting .s2iignore: %v", serr)
    58  		}
    59  		if strings.HasPrefix(pattern, "!") {
    60  			pattern = strings.Replace(pattern, "!", "", 1)
    61  			t.Logf("Noting ignore pattern %q", pattern)
    62  			filesToIgnore[pattern] = pattern
    63  		}
    64  	}
    65  
    66  	// create slices the store files to create, maps for files which should be deleted, files which should be kept
    67  	filesToCreate := []string{}
    68  	filesToDelCheck := make(map[string]string)
    69  	for _, fileToDel := range filesToDel {
    70  		filesToDelCheck[fileToDel] = fileToDel
    71  		filesToCreate = append(filesToCreate, fileToDel)
    72  	}
    73  	filesToKeepCheck := make(map[string]string)
    74  	for _, fileToKeep := range filesToKeep {
    75  		filesToKeepCheck[fileToKeep] = fileToKeep
    76  		filesToCreate = append(filesToCreate, fileToKeep)
    77  	}
    78  
    79  	// create files for test
    80  	for _, fileToCreate := range filesToCreate {
    81  		fbpath := filepath.Join(dpath, fileToCreate)
    82  
    83  		// ensure any subdirs off working dir exist
    84  		dirpath := filepath.Dir(fbpath)
    85  		derr := os.MkdirAll(dirpath, 0777)
    86  		if derr != nil && !os.IsExist(derr) {
    87  			t.Errorf("Problem creating subdirs %q: %v", dirpath, derr)
    88  		}
    89  		t.Logf("Going to create file %q", fbpath)
    90  		fbfile, fberr := os.Create(fbpath)
    91  		if fberr != nil {
    92  			t.Errorf("Problem creating test file: %v", fberr)
    93  		}
    94  		fbfile.Close()
    95  	}
    96  
    97  	// run ignorer algorithm
    98  	ignorer := &DockerIgnorer{}
    99  	ignorer.Ignore(c)
   100  
   101  	// check if filesToDel, minus ignores, are gone, and filesToKeep are still there
   102  	for _, fileToCheck := range filesToCreate {
   103  		fbpath := filepath.Join(dpath, fileToCheck)
   104  		t.Logf("Evaluating file %q from dir %q and file to check %q", fbpath, dpath, fileToCheck)
   105  
   106  		// see if file still exists or not
   107  		ofile, oerr := os.Open(fbpath)
   108  		defer ofile.Close()
   109  		var fileExists bool
   110  		if oerr == nil {
   111  			fileExists = true
   112  			t.Logf("The file %q exists after Ignore was run", fbpath)
   113  		} else {
   114  			if os.IsNotExist(oerr) {
   115  				t.Logf("The file %q does not exist after Ignore was run", fbpath)
   116  				fileExists = false
   117  			} else {
   118  				t.Errorf("Could not verify existence of %q: %v", fbpath, oerr)
   119  			}
   120  		}
   121  
   122  		_, iok := filesToIgnore[fileToCheck]
   123  		_, kok := filesToKeepCheck[fileToCheck]
   124  		_, dok := filesToDelCheck[fileToCheck]
   125  
   126  		// if file present, verify it is in ignore or keep list, and not in del list
   127  		if fileExists {
   128  			if iok {
   129  				t.Logf("validated ignored file is still present: %q", fileToCheck)
   130  				continue
   131  			}
   132  			if kok {
   133  				t.Logf("validated file to keep is still present: %q", fileToCheck)
   134  				continue
   135  			}
   136  			if dok {
   137  				t.Errorf("file which was cited to be deleted by caller to runTest exists: %q", fileToCheck)
   138  				continue
   139  			}
   140  			// if here, something unexpected
   141  			t.Errorf("file %q not in ignore / keep / del list !?!?!?!?", fileToCheck)
   142  		} else {
   143  			if dok {
   144  				t.Logf("file which should have been deleted is in fact gone: %q", fileToCheck)
   145  				continue
   146  			}
   147  			if iok {
   148  				t.Errorf("file put into ignore list does not exist: %q", fileToCheck)
   149  				continue
   150  			}
   151  			if kok {
   152  				t.Errorf("file passed in with keep list does not exist: %q", fileToCheck)
   153  				continue
   154  			}
   155  			// if here, then something unexpected happened
   156  			t.Errorf("file %q not in ignore / keep / del list !?!?!?!?", fileToCheck)
   157  		}
   158  	}
   159  }
   160  
   161  func TestBlankLine(t *testing.T) {
   162  	baseTest(t, []string{"foo.bar\n", "\n", "bar.baz\n"}, []string{"foo.bar", "bar.baz"}, []string{"foo.baz"})
   163  }
   164  
   165  func TestSingleIgnore(t *testing.T) {
   166  	baseTest(t, []string{"foo.bar\n"}, []string{"foo.bar"}, []string{})
   167  }
   168  
   169  func TestWildcardIgnore(t *testing.T) {
   170  	baseTest(t, []string{"foo.*\n"}, []string{"foo.a", "foo.b"}, []string{})
   171  }
   172  
   173  func TestExclusion(t *testing.T) {
   174  	baseTest(t, []string{"foo.*\n", "!foo.a"}, []string{"foo.b"}, []string{"foo.a"})
   175  }
   176  
   177  func TestSubDirs(t *testing.T) {
   178  	baseTest(t, []string{"*/foo.a\n"}, []string{"asdf/foo.a"}, []string{"foo.a"})
   179  }
   180  
   181  func TestBasicDelKeepMix(t *testing.T) {
   182  	baseTest(t, []string{"foo.bar\n"}, []string{"foo.bar"}, []string{"bar.foo"})
   183  }
   184  
   185  /*
   186  Per the docker user guide, with a docker ignore list of:
   187  
   188      LICENSE.*
   189      !LICENSE.md
   190      *.md
   191  
   192  LICENSE.MD will NOT be kept, as *.md overrides !LICENSE.md
   193  */
   194  func TestExcludeOverride(t *testing.T) {
   195  	baseTest(t, []string{"LICENSE.*\n", "!LICENSE.md\n", "*.md"}, []string{"LICENSE.foo", "LICENSE.md"}, []string{"foo.bar"})
   196  }
   197  
   198  func TestExclusionWithWildcard(t *testing.T) {
   199  	baseTest(t, []string{"*.bar\n", "!foo.*"}, []string{"boo.bar", "bar.bar"}, []string{"foo.bar"})
   200  }
   201  
   202  func TestHopelessExclusion(t *testing.T) {
   203  	baseTest(t, []string{"!LICENSE.md\n", "LICENSE.*"}, []string{"LICENSE.md"}, []string{})
   204  }