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