github.com/atrn/dcc@v0.0.0-20220806184050-4470d2553272/find_test.go (about)

     1  // dcc - dependency-driven C/C++ compiler front end
     2  //
     3  // Copyright © A.Newman 2015.
     4  //
     5  // This source code is released under version 2 of the  GNU Public License.
     6  // See the file LICENSE for details.
     7  //
     8  
     9  package main
    10  
    11  import (
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  )
    17  
    18  func invalidateStatCache() {
    19  	statCacheMutex.Lock()
    20  	statCache = make(map[string]os.FileInfo)
    21  	statCacheMutex.Unlock()
    22  }
    23  
    24  const (
    25  	testDataDir  = "testdata"
    26  	testFilename = "testfile"
    27  )
    28  
    29  var (
    30  	testProjectDccDir   = filepath.Join(testDataDir, DefaultDccDir)
    31  	testProjectRootDir  = filepath.Join(testDataDir, "root")
    32  	testProjectChildDir = filepath.Join(testProjectRootDir, "child")
    33  )
    34  
    35  func makeTestDirs(t *testing.T) {
    36  	mustMkdir := func(path string) {
    37  		if err := os.MkdirAll(path, 0777); err != nil {
    38  			t.Fatal(err)
    39  		}
    40  	}
    41  	mustMkdir(testProjectRootDir)
    42  	mustMkdir(testProjectChildDir)
    43  	mustMkdir(testProjectDccDir)
    44  }
    45  
    46  func removeTestDirs(t *testing.T) {
    47  	if err := os.RemoveAll("testdata"); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  }
    51  
    52  func makeFileWithContent(t *testing.T, path, content string) {
    53  	if err := ioutil.WriteFile(path, []byte(content), 0666); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  }
    57  
    58  func makeFile(t *testing.T, path string) {
    59  	makeFileWithContent(t, path, path)
    60  }
    61  
    62  func checkFile(t *testing.T, path, content string) {
    63  	if data, err := os.ReadFile(path); err != nil {
    64  		t.Fatal(err)
    65  	} else if s := string(data); s != content {
    66  		t.Fatalf("%s: content (%q) does not match expected content (%q)", path, s, content)
    67  	}
    68  }
    69  
    70  func setupTest(t *testing.T) {
    71  	removeTestDirs(t)
    72  	makeTestDirs(t)
    73  }
    74  
    75  func singleFileTestCase(t *testing.T, dirname, filename, cwd string) {
    76  	invalidateStatCache()
    77  
    78  	fullpath := filepath.Join(dirname, filename)
    79  	abspath, err := filepath.Abs(fullpath)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	makeFile(t, fullpath)
    84  
    85  	defer func(name string) {
    86  		if err := os.Remove(name); err != nil {
    87  			t.Fatal(err)
    88  		}
    89  	}(fullpath)
    90  
    91  	defer func(dir string) {
    92  		if err := os.Chdir(dir); err != nil {
    93  			t.Fatal(err)
    94  		}
    95  	}(MustGetwd())
    96  
    97  	if err = os.Chdir(cwd); err != nil {
    98  		t.Fatal(err)
    99  	} else if foundpath, found := FindFile(filename); !found {
   100  		t.Fatalf("%q not found", filename)
   101  	} else if foundpath != abspath {
   102  		t.Fatalf("found %q which is not the expected %q", foundpath, fullpath)
   103  	} else {
   104  		checkFile(t, foundpath, fullpath)
   105  	}
   106  }
   107  
   108  func Test_FindFile_DirectorySearching(t *testing.T) {
   109  	setupTest(t)
   110  	defer removeTestDirs(t)
   111  	singleFileTestCase(t, testProjectChildDir, testFilename, testProjectChildDir)
   112  	singleFileTestCase(t, testProjectRootDir, testFilename, testProjectChildDir)
   113  	singleFileTestCase(t, testProjectDccDir, testFilename, testProjectChildDir)
   114  }
   115  
   116  func Test_FindFile_PlatformSpecificSearches(t *testing.T) {
   117  	setupTest(t)
   118  	defer removeTestDirs(t)
   119  	singleFileTestCase(t, testProjectChildDir, OsSpecificFilename(testFilename), testProjectChildDir)
   120  	singleFileTestCase(t, testProjectDccDir, OsAndArchSpecificFilename(testFilename), testProjectChildDir)
   121  }