github.com/n3integration/conseil@v0.1.1/actions/app_test.go (about)

     1  package actions
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  	"text/template"
    13  
    14  	"github.com/n3integration/conseil"
    15  )
    16  
    17  var (
    18  	update    bool
    19  	templates *template.Template
    20  )
    21  
    22  func init() {
    23  	flag.BoolVar(&update, "update", false, "update .golden files")
    24  	flag.Parse()
    25  
    26  	templates = parseTemplates()
    27  }
    28  
    29  func TestParseWebAppTemplates(t *testing.T) {
    30  	apps := listApps()
    31  	templateList := templates.Templates()
    32  
    33  	if len(templateList) < len(apps) {
    34  		t.Fatalf("insufficient number of templates parsed. expected at least %d; actual %d", len(apps), len(templateList))
    35  	}
    36  
    37  	for _, app := range apps {
    38  		found := false
    39  		for _, tpl := range templateList {
    40  			if strings.Contains(tpl.Name(), app) {
    41  				found = true
    42  			}
    43  		}
    44  		if !found {
    45  			t.Errorf("failed to locate a parsed tpl file for %s", app)
    46  		}
    47  	}
    48  }
    49  
    50  func TestCreateWebApp(t *testing.T) {
    51  	stageTest(t, func(t *testing.T) {
    52  		tests := []struct {
    53  			Framework string
    54  			Host      string
    55  			Port      int
    56  			Error     bool
    57  		}{
    58  			{"echo", "localhost", 8080, false},
    59  			{"gin", "localhost", 8080, false},
    60  			{"grpc", "localhost", 9000, false},
    61  			{"iris", "localhost", 8080, false},
    62  			{"ozzo", "localhost", 8080, false},
    63  			{"eggio", "localhost", 8080, true},
    64  		}
    65  
    66  		for _, test := range tests {
    67  			framework = test.Framework
    68  			host = test.Host
    69  			port = test.Port
    70  			err := createWebApp(templates)
    71  
    72  			if test.Error {
    73  				if err == nil {
    74  					t.Error("expected test to generate an error")
    75  				}
    76  				break
    77  			}
    78  
    79  			if err != nil {
    80  				t.Errorf("failed to create %s web application: %s", framework, err)
    81  			}
    82  
    83  			actual, err := ioutil.ReadFile(filepath.Join(wd, "app.go"))
    84  			golden := filepath.Join("testdata", test.Framework+".golden")
    85  			if update {
    86  				ioutil.WriteFile(golden, actual, 0644)
    87  			}
    88  
    89  			expected, _ := ioutil.ReadFile(golden)
    90  			if !bytes.Equal(actual, expected) {
    91  				t.Fatalf("generated %s application contents did not match: \n%s", test.Framework, actual)
    92  			}
    93  		}
    94  	})
    95  }
    96  
    97  func TestStageMigrations(t *testing.T) {
    98  	stageTest(t, func(t *testing.T) {
    99  		if err := stageMigrations(templates); err != nil {
   100  			t.Errorf("failed to stage migrations: %s", err)
   101  		}
   102  
   103  		if actual := conseil.FileCount(wd, ".*\\.sql"); actual != 2 {
   104  			t.Errorf("expected 2 migration files; actual %d", actual)
   105  		}
   106  	})
   107  }
   108  
   109  func TestSetupDb(t *testing.T) {
   110  	stageTest(t, func(t *testing.T) {
   111  		tests := []struct {
   112  			Driver string
   113  			Error  bool
   114  		}{
   115  			{"postgres", false},
   116  			{"sqlite3", false},
   117  			{"mysql", true},
   118  		}
   119  
   120  		for _, test := range tests {
   121  			driver = test.Driver
   122  			err := setupDb(templates)
   123  
   124  			if test.Error {
   125  				if err == nil {
   126  					t.Error("expected test to generate an error")
   127  				}
   128  				break
   129  			}
   130  
   131  			if err != nil {
   132  				t.Errorf("failed to setup database file: %s", err)
   133  			}
   134  
   135  			actual, err := ioutil.ReadFile(filepath.Join(wd, "sql", "sql.go"))
   136  			golden := filepath.Join("testdata", test.Driver+".golden")
   137  			if update {
   138  				ioutil.WriteFile(golden, actual, 0644)
   139  			}
   140  
   141  			expected, _ := ioutil.ReadFile(golden)
   142  			if !bytes.Equal(actual, expected) {
   143  				t.Fatalf("generated %s application contents did not match: \n%s", test.Driver, actual)
   144  			}
   145  		}
   146  	})
   147  }
   148  
   149  func TestDepInit(t *testing.T) {
   150  	if _, err := exec.LookPath("dep"); err != nil {
   151  		t.Log("dep not found, skipping")
   152  		t.Skip()
   153  	}
   154  
   155  	// TODO
   156  	// stageTest(t, func(t *testing.T) {
   157  	// 	if err := os.Chdir(wd); err != nil {
   158  	// 		t.Fatalf("err: %s", err)
   159  	// 	}
   160  	//
   161  	// 	output, err := depInit()
   162  	// 	if err != nil {
   163  	// 		t.Errorf("failed to initialize dep: %s\n%s", err, output)
   164  	// 	}
   165  	// })
   166  }
   167  
   168  func TestGitInit(t *testing.T) {
   169  	if _, err := exec.LookPath("git"); err != nil {
   170  		t.Log("git not found, skipping")
   171  		t.Skip()
   172  	}
   173  
   174  	stageTest(t, func(t *testing.T) {
   175  		if err := os.Chdir(wd); err != nil {
   176  			t.Fatalf("err: %s", err)
   177  		}
   178  
   179  		_, err := gitInit(templates)
   180  		if err != nil {
   181  			t.Errorf("failed to initialize git: %s", err)
   182  		}
   183  
   184  		if actual := conseil.FileCount(wd, ".*\\.gitignore"); actual != 1 {
   185  			t.Errorf("expected 1 .gitignore file; actual %d", actual)
   186  		}
   187  	})
   188  }
   189  
   190  func stageTest(t *testing.T, fn func(*testing.T)) {
   191  	var cleanup func()
   192  	wd, cleanup = conseil.StageTestDir(t)
   193  	defer cleanup()
   194  	fn(t)
   195  }