github.com/nvi-inc/fsgo@v0.2.1/fsgo_test.go (about)

     1  package fs_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	fs "github.com/nvi-inc/fsgo"
    10  )
    11  
    12  func TestInstalledVersionFromMakefile(t *testing.T) {
    13  	const testVersion = "9.11.19"
    14  	const makefile = `VERSION = 9
    15  SUBLEVEL = 11
    16  PATCHLEVEL = 19
    17  FS_VERSION = $(VERSION).$(SUBLEVEL).$(PATCHLEVEL)
    18  export VERSION SUBLEVEL PATCHLEVEL FS_VERSION
    19  	`
    20  	must := func(s string, err error) {
    21  		if err != nil {
    22  			t.Fatal(s, err)
    23  		}
    24  	}
    25  
    26  	root, err := ioutil.TempDir("", "fstest*")
    27  	must("setup test root", err)
    28  
    29  	t.Cleanup(func() {
    30  		os.RemoveAll(root)
    31  	})
    32  
    33  	path := root + "/fs"
    34  	must("setup dir", os.Mkdir(path, 0o700))
    35  	must("writing test file", ioutil.WriteFile(path+"/Makefile", []byte(makefile), 0o600))
    36  
    37  	version, err := fs.InstalledVersionFromMakefile(path)
    38  	if err != nil {
    39  		t.Error("unexpected error", err)
    40  	}
    41  	if version != testVersion {
    42  		t.Errorf("expected version %q, got version %q", testVersion, version)
    43  	}
    44  }
    45  
    46  func TestInstalledVersionFromPath(t *testing.T) {
    47  	const testVersion = "10.0.0"
    48  	must := func(s string, err error) {
    49  		if err != nil {
    50  			t.Fatal(s, err)
    51  		}
    52  	}
    53  
    54  	root, err := ioutil.TempDir("", "fstest*")
    55  	must("setup test root", err)
    56  
    57  	t.Cleanup(func() {
    58  		os.RemoveAll(root)
    59  	})
    60  
    61  	path := root + "/fs"
    62  
    63  	t.Run("not a symbolic link", func(t *testing.T) {
    64  		must("setup dir", os.Mkdir(path, 0o600))
    65  		_, err := fs.InstalledVersionFromPath(path)
    66  		if err == nil {
    67  			t.Error("expected an error")
    68  		}
    69  		must("remove dir", os.Remove(path))
    70  	})
    71  
    72  	t.Run("reject extra junk", func(t *testing.T) {
    73  		realpath := root + "/fs-" + testVersion + "-test"
    74  		must("setup test path", os.Mkdir(realpath, 0o600))
    75  		must("setup symlink", os.Symlink(realpath, path))
    76  
    77  		version, err := fs.InstalledVersionFromPath(path)
    78  		if err == nil {
    79  			t.Errorf("expected an error instead got %q", version)
    80  		}
    81  		must("cleanup realpath", os.Remove(realpath))
    82  		must("cleanup path", os.Remove(path))
    83  	})
    84  
    85  	t.Run("detect good version", func(t *testing.T) {
    86  		realpath := root + "/fs-" + testVersion
    87  		must("setup test path", os.Mkdir(realpath, 0o600))
    88  		must("setup symlink", os.Symlink(realpath, path))
    89  
    90  		version, err := fs.InstalledVersionFromPath(path)
    91  		if err != nil {
    92  			t.Error("unexpected error", err)
    93  		}
    94  		if version != testVersion {
    95  			t.Errorf("expected version %q, got version %q", testVersion, version)
    96  		}
    97  	})
    98  
    99  }
   100  
   101  func TestInstalledVersionFromGit(t *testing.T) {
   102  	t.Run("nonexistant path", func(t *testing.T) {
   103  		_, err := fs.InstalledVersionFromGit("/thispathshouldnotexit")
   104  		if !os.IsNotExist(err) {
   105  			t.Error("unexpected error", err)
   106  		}
   107  	})
   108  
   109  	must := func(s string, err error) {
   110  		if err != nil {
   111  			t.Fatal(s, err)
   112  		}
   113  	}
   114  
   115  	path, err := ioutil.TempDir("", "fstest*")
   116  	t.Cleanup(func() {
   117  		os.RemoveAll(path)
   118  	})
   119  
   120  	const tagVersion = "10.0.0"
   121  
   122  	must("setting up testdir", err)
   123  
   124  	t.Run("not a git repo", func(t *testing.T) {
   125  		_, err := fs.InstalledVersionFromGit(path)
   126  		if err != fs.ErrNotGitDir {
   127  			t.Error("unexpected error", err)
   128  		}
   129  	})
   130  
   131  	must("git init", exec.Command("git", "init", path).Run())
   132  	git := func(args ...string) {
   133  		args = append([]string{
   134  			"--git-dir=" + path + "/.git",
   135  			"--work-tree=" + path,
   136  		}, args...)
   137  		if err := exec.Command("git", args...).Run(); err != nil {
   138  			t.Fatalf("while calling git with args %v: %v", args, err)
   139  		}
   140  	}
   141  
   142  	must("writing test file", ioutil.WriteFile(path+"/test.txt", []byte("test"), 0644))
   143  	git("add", path+"/test.txt")
   144  	git("commit", "-m", "test")
   145  	git("tag", tagVersion)
   146  
   147  	version, err := fs.InstalledVersionFromGit(path)
   148  
   149  	if err != nil {
   150  		t.Fatal("running InstalledVersionFromGit", err)
   151  	}
   152  	if version != tagVersion {
   153  		t.Errorf("version %q does not match expected %q", version, tagVersion)
   154  
   155  	}
   156  }
   157  
   158  func TestSupportedVersions(t *testing.T) {
   159  	vs := fs.SupportedVersions()
   160  	if len(vs) == 0 {
   161  		t.Errorf("no supported versions")
   162  	}
   163  }
   164  
   165  func TestAttach(t *testing.T) {
   166  	// Need to be run with the FS installed and setup
   167  	_, err := fs.Attach()
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  
   172  }