github.com/d0sbit/gocode@v0.0.0-20211001144653-a968ce917518/cmd/gocode_mongocrud/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestMaineExec(t *testing.T) {
    12  
    13  	// tests with execution of `go test` - requires "docker run ..." etc to work
    14  
    15  	var modDir string
    16  	modDir = t.TempDir()
    17  	// modDir, _ = ioutil.TempDir("", "TestMaine")
    18  	t.Logf("modDir: %s", modDir)
    19  	must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755))
    20  	must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644))
    21  	must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a
    22  
    23  import "go.mongodb.org/mongo-driver/bson/primitive"
    24  
    25  type A struct {
    26  	ID primitive.ObjectID `+"`bson:\"_id\"`"+`
    27  	Name string `+"`bson:\"name\"`"+`
    28  }
    29  `), 0644))
    30  
    31  	// run the code generator
    32  	must(t, os.Chdir(modDir))
    33  	flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError)
    34  	ret := maine(flset, []string{"-package=a", "-type=A"})
    35  	if ret != 0 {
    36  		t.Errorf("ret = %d", ret)
    37  	}
    38  
    39  	cmd := exec.Command("go", "get", "./...")
    40  	cmd.Dir = modDir
    41  	b, err := cmd.CombinedOutput()
    42  	t.Logf("go get cmd output: %s", b)
    43  	must(t, err)
    44  
    45  	cmd = exec.Command("go", "test", "-v", "./...")
    46  	cmd.Dir = modDir
    47  	b, err = cmd.CombinedOutput()
    48  	t.Logf("test cmd output: %s", b)
    49  	must(t, err)
    50  
    51  }
    52  
    53  func TestMaineDryRun(t *testing.T) {
    54  
    55  	var modDir string
    56  	modDir = t.TempDir()
    57  	// modDir, _ = ioutil.TempDir("", "TestMaine")
    58  	t.Logf("modDir: %s", modDir)
    59  	must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755))
    60  	must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644))
    61  	must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a
    62  
    63  import "go.mongodb.org/mongo-driver/bson/primitive"
    64  
    65  type A struct {
    66  	ID primitive.ObjectID `+"`bson:\"_id\"`"+`
    67  	Name string `+"`bson:\"name\"`"+`
    68  }
    69  `), 0644))
    70  
    71  	// run the code generator
    72  	must(t, os.Chdir(modDir))
    73  	flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError)
    74  	ret := maine(flset, []string{"-package=a", "-type=A", "-dry-run=html", "-json"})
    75  	if ret != 0 {
    76  		t.Errorf("ret = %d", ret)
    77  	}
    78  
    79  }
    80  
    81  func TestMaineSubDirs(t *testing.T) {
    82  
    83  	// make sure subdirs in the package folder don't cause an issue
    84  
    85  	var modDir string
    86  	modDir = t.TempDir()
    87  	// modDir, _ = ioutil.TempDir("", "TestMaine")
    88  	t.Logf("modDir: %s", modDir)
    89  	must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755))
    90  	must(t, os.Mkdir(filepath.Join(modDir, "a/tmp"), 0755))
    91  	must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644))
    92  	must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a
    93  
    94  import "go.mongodb.org/mongo-driver/bson/primitive"
    95  
    96  type A struct {
    97  	ID primitive.ObjectID `+"`bson:\"_id\"`"+`
    98  	Name string `+"`bson:\"name\"`"+`
    99  }
   100  `), 0644))
   101  
   102  	// run the code generator
   103  	must(t, os.Chdir(modDir))
   104  	flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError)
   105  	ret := maine(flset, []string{"-package=a", "-type=A", "-dry-run=html", "-json"})
   106  	if ret != 0 {
   107  		t.Errorf("ret = %d", ret)
   108  	}
   109  
   110  }
   111  
   112  func TestMaineOneDir(t *testing.T) {
   113  
   114  	// confirm that it works when the package dir is empty
   115  	// and everything is directly in the module dir
   116  
   117  	var modDir string
   118  	modDir = t.TempDir()
   119  	// modDir, _ = ioutil.TempDir("", "TestMaine")
   120  	t.Logf("modDir: %s", modDir)
   121  	must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644))
   122  	must(t, os.WriteFile(filepath.Join(modDir, "types.go"), []byte(`package a
   123  
   124  import "go.mongodb.org/mongo-driver/bson/primitive"
   125  
   126  type A struct {
   127  	ID primitive.ObjectID `+"`bson:\"_id\"`"+`
   128  	Name string `+"`bson:\"name\"`"+`
   129  }
   130  `), 0644))
   131  
   132  	// run the code generator
   133  	must(t, os.Chdir(modDir))
   134  	flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError)
   135  	ret := maine(flset, []string{"-package=.", "-type=A", "-dry-run=html", "-json"})
   136  	if ret != 0 {
   137  		t.Errorf("ret = %d", ret)
   138  	}
   139  
   140  }
   141  
   142  func must(t *testing.T, err error) {
   143  	t.Helper()
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  }