github.com/hattya/nazuna@v0.7.1-0.20240331055452-55e14c275c1c/vcs_test.go (about)

     1  //
     2  // nazuna :: vcs_test.go
     3  //
     4  //   Copyright (c) 2013-2022 Akinori Hattori <hattya@gmail.com>
     5  //
     6  //   SPDX-License-Identifier: MIT
     7  //
     8  
     9  package nazuna_test
    10  
    11  import (
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/hattya/nazuna"
    17  )
    18  
    19  type testVCS struct {
    20  	nazuna.BaseVCS
    21  }
    22  
    23  func newTest(ui nazuna.UI, dir string) nazuna.VCS {
    24  	return &testVCS{nazuna.BaseVCS{
    25  		Name: "name",
    26  		Cmd:  "cmd",
    27  		UI:   ui,
    28  		Dir:  dir,
    29  	}}
    30  }
    31  
    32  func init() {
    33  	nazuna.RegisterVCS("test", ".test", newTest)
    34  }
    35  
    36  func TestRegisterVCSPanic(t *testing.T) {
    37  	func() {
    38  		defer func() {
    39  			if recover() == nil {
    40  				t.Error("expected panic")
    41  			}
    42  		}()
    43  
    44  		nazuna.RegisterVCS("test", ".test", newTest)
    45  	}()
    46  
    47  	func() {
    48  		defer func() {
    49  			if recover() == nil {
    50  				t.Error("expected panic")
    51  			}
    52  		}()
    53  
    54  		nazuna.RegisterVCS("TEST", ".test", newTest)
    55  	}()
    56  }
    57  
    58  func TestFindVCS(t *testing.T) {
    59  	vcs, err := nazuna.FindVCS(nil, "hg", "")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	switch vcs := vcs.(type) {
    64  	case *nazuna.Mercurial:
    65  		if g, e := vcs.Dir, ""; g != e {
    66  			t.Errorf("VCS.Dir = %q, expected %q", g, e)
    67  		}
    68  	default:
    69  		t.Errorf("expected *Mercurial, got %T", vcs)
    70  	}
    71  
    72  	if _, err := nazuna.FindVCS(nil, "cvs", ""); err == nil {
    73  		t.Error("expected error")
    74  	}
    75  }
    76  
    77  func TestVCSFor(t *testing.T) {
    78  	dir := sandbox(t)
    79  
    80  	if err := mkdir(".git"); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	vcs, err := nazuna.VCSFor(nil, dir)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	switch vcs := vcs.(type) {
    88  	case *nazuna.Git:
    89  		if g, e := vcs.Dir, dir; g != e {
    90  			t.Errorf("VCS.Dir = %q, expected %q", g, e)
    91  		}
    92  	default:
    93  		t.Fatalf("expected *Git, got %T", vcs)
    94  	}
    95  
    96  	if err := os.Remove(".git"); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	if _, err = nazuna.VCSFor(nil, dir); err == nil {
   100  		t.Error("expected error")
   101  	}
   102  }
   103  
   104  func TestBaseVCS(t *testing.T) {
   105  	vcs, err := nazuna.FindVCS(nil, "test", "")
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	if g, e := vcs.String(), "name"; g != e {
   111  		t.Errorf("VCS.String() = %q, expected %q", g, e)
   112  	}
   113  	if err := vcs.Init("dir"); err == nil {
   114  		t.Error("expected error")
   115  	}
   116  	if err := vcs.Clone("src", "dst"); err == nil {
   117  		t.Error("expected error")
   118  	}
   119  	if err := vcs.Add("a", "b", "c"); err == nil {
   120  		t.Error("expected error")
   121  	}
   122  	if cmd := vcs.List("a", "b", "c"); cmd != nil {
   123  		t.Errorf("expected nil, got %T", cmd)
   124  	}
   125  	if err := vcs.Update(); err == nil {
   126  		t.Error("expected error")
   127  	}
   128  }
   129  
   130  func TestGitVCS(t *testing.T) {
   131  	testVCSImpl(t, "git", func(vcs nazuna.VCS) (err error) {
   132  		for n, v := range map[string]string{
   133  			"user.name":  "Nazuna",
   134  			"user.email": "nazuna@example.com",
   135  		} {
   136  			if err = vcs.Exec("config", "--local", n, v); err != nil {
   137  				break
   138  			}
   139  		}
   140  		return
   141  	})
   142  }
   143  
   144  func TestMercurialVCS(t *testing.T) {
   145  	testVCSImpl(t, "hg", func(vcs nazuna.VCS) error {
   146  		dir := vcs.(*nazuna.Mercurial).Dir
   147  		data := "[ui]\nusername = Nazuna <nazuna@example.com>\n"
   148  		return os.WriteFile(filepath.Join(dir, ".hg", "hgrc"), []byte(data), 0o666)
   149  	})
   150  }
   151  
   152  func testVCSImpl(t *testing.T, cmd string, config func(nazuna.VCS) error) {
   153  	sandbox(t)
   154  
   155  	ui := new(testUI)
   156  	vcs, err := nazuna.FindVCS(ui, cmd, "")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	if err := vcs.Init("repo"); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	if err := vcs.Clone("repo", "wc"); err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	vcs, err = nazuna.FindVCS(ui, cmd, "repo")
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  	if err := vcs.Update(); err == nil {
   172  		t.Error("expected error")
   173  	}
   174  	if err := config(vcs); err != nil {
   175  		t.Fatal(err)
   176  	}
   177  	if err := touch("repo", "file"); err != nil {
   178  		t.Fatal(err)
   179  	}
   180  	if err := mkdir("repo", "dir"); err != nil {
   181  		t.Fatal(err)
   182  	}
   183  	if err := touch("repo", "dir", "file"); err != nil {
   184  		t.Fatal(err)
   185  	}
   186  	if err := vcs.Add("."); err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	if err := vcs.Exec("commit", "-m", "."); err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	vcs, err = nazuna.FindVCS(ui, cmd, "wc")
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  	ui.Reset()
   198  	if err := ui.Exec(vcs.List(".")); err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	if g, e := ui.String(), ""; g != e {
   202  		t.Errorf("expected %q, got %q", e, g)
   203  	}
   204  	if err := vcs.Update(); err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	ui.Reset()
   208  	if err := ui.Exec(vcs.List(".")); err != nil {
   209  		t.Fatal(err)
   210  	}
   211  	if g, e := ui.String(), "dir/file\nfile\n"; g != e {
   212  		t.Errorf("expected %q, got %q", e, g)
   213  	}
   214  }