github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/command/compile_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/otto/app"
     9  	"github.com/hashicorp/otto/appfile"
    10  	"github.com/hashicorp/otto/appfile/detect"
    11  	"github.com/hashicorp/otto/foundation"
    12  	"github.com/hashicorp/otto/otto"
    13  	"github.com/mitchellh/cli"
    14  )
    15  
    16  func TestCompile(t *testing.T) {
    17  	core := otto.TestCoreConfig(t)
    18  	infra := otto.TestInfra(t, "test", core)
    19  	appImpl := otto.TestApp(t, app.Tuple{"test", "test", "test"}, core)
    20  	ui := new(cli.MockUi)
    21  	c := &CompileCommand{
    22  		Meta: Meta{
    23  			CoreConfig: core,
    24  			Ui:         ui,
    25  		},
    26  	}
    27  
    28  	dir := fixtureDir("compile-basic")
    29  	defer os.Remove(filepath.Join(dir, ".ottoid"))
    30  	defer testChdir(t, dir)()
    31  
    32  	args := []string{}
    33  	if code := c.Run(args); code != 0 {
    34  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    35  	}
    36  
    37  	if !infra.CompileCalled {
    38  		t.Fatal("Compile should be called")
    39  	}
    40  	if !appImpl.CompileCalled {
    41  		t.Fatal("Compile should be called")
    42  	}
    43  }
    44  
    45  func TestCompile_appfileDir(t *testing.T) {
    46  	core := otto.TestCoreConfig(t)
    47  	infra := otto.TestInfra(t, "aws", core)
    48  	otto.TestFoundation(t, foundation.Tuple{"consul", "aws", "simple"}, core)
    49  	otto.TestApp(t, app.Tuple{"test", "aws", "simple"}, core)
    50  
    51  	ui := new(cli.MockUi)
    52  	detectors := []*detect.Detector{
    53  		&detect.Detector{
    54  			Type: "test",
    55  			File: []string{"main.txt"},
    56  		},
    57  	}
    58  
    59  	c := &CompileCommand{
    60  		Meta: Meta{
    61  			CoreConfig: core,
    62  			Ui:         ui,
    63  		},
    64  		Detectors: detectors,
    65  	}
    66  
    67  	dir := fixtureDir("compile-appfile-dir")
    68  	defer testChdir(t, dir)()
    69  
    70  	args := []string{}
    71  	if code := c.Run(args); code != 0 {
    72  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    73  	}
    74  
    75  	if !infra.CompileCalled {
    76  		t.Fatal("Compile should be called")
    77  	}
    78  }
    79  
    80  func TestCompile_detectPriority(t *testing.T) {
    81  	core := otto.TestCoreConfig(t)
    82  	otto.TestInfra(t, "aws", core)
    83  	otto.TestFoundation(t, foundation.Tuple{"consul", "aws", "simple"}, core)
    84  	appImpl := otto.TestApp(t, app.Tuple{"test", "aws", "simple"}, core)
    85  	ui := new(cli.MockUi)
    86  	detectors := []*detect.Detector{
    87  		&detect.Detector{
    88  			Type: "invalid",
    89  			File: []string{"test-file"},
    90  		},
    91  		&detect.Detector{
    92  			Type:     "test",
    93  			File:     []string{"test-file"},
    94  			Priority: 10,
    95  		},
    96  	}
    97  	c := &CompileCommand{
    98  		Meta: Meta{
    99  			CoreConfig: core,
   100  			Ui:         ui,
   101  		},
   102  		Detectors: detectors,
   103  	}
   104  
   105  	dir := fixtureDir("compile-no-appfile")
   106  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   107  	defer testChdir(t, dir)()
   108  
   109  	args := []string{}
   110  	if code := c.Run(args); code != 0 {
   111  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   112  	}
   113  
   114  	if !appImpl.CompileCalled {
   115  		t.Fatal("Compile should be called")
   116  	}
   117  }
   118  func TestCompile_implicit(t *testing.T) {
   119  	dir := fixtureDir("compile-implicit")
   120  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   121  	defer testChdir(t, dir)()
   122  
   123  	core := otto.TestCoreConfig(t)
   124  	otto.TestFoundation(t, foundation.Tuple{"consul", "aws", "simple"}, core)
   125  	infra := otto.TestInfra(t, "aws", core)
   126  	appImpl := otto.TestApp(t, app.Tuple{"test", "aws", "simple"}, core)
   127  	childAppImpl := otto.TestApp(t, app.Tuple{"child", "aws", "simple"}, core)
   128  
   129  	appImpl.ImplicitResult = &appfile.File{
   130  		Application: &appfile.Application{
   131  			Dependencies: []*appfile.Dependency{
   132  				&appfile.Dependency{
   133  					Source: "child",
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	ui := new(cli.MockUi)
   140  	detectors := []*detect.Detector{
   141  		&detect.Detector{
   142  			Type: "test",
   143  			File: []string{"test-file"},
   144  		},
   145  	}
   146  
   147  	c := &CompileCommand{
   148  		Meta: Meta{
   149  			CoreConfig: core,
   150  			Ui:         ui,
   151  		},
   152  		Detectors: detectors,
   153  	}
   154  
   155  	args := []string{}
   156  	if code := c.Run(args); code != 0 {
   157  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   158  	}
   159  
   160  	if !infra.CompileCalled {
   161  		t.Fatal("Compile should be called")
   162  	}
   163  	if !appImpl.CompileCalled {
   164  		t.Fatal("Compile should be called")
   165  	}
   166  	if !childAppImpl.CompileCalled {
   167  		t.Fatal("Compile should be called")
   168  	}
   169  }
   170  
   171  func TestCompile_noExplicitType(t *testing.T) {
   172  	core := otto.TestCoreConfig(t)
   173  	infra := otto.TestInfra(t, "aws", core)
   174  	appImpl := otto.TestApp(t, app.Tuple{"test-detection-merge", "aws", "simple"}, core)
   175  	appImpl.CompileFunc = func(ctx *app.Context) (*app.CompileResult, error) {
   176  		if ctx.Application == nil {
   177  			t.Fatal("application unexpectedly nil")
   178  		}
   179  		if ctx.Application.Name != "compile-no-explicit-type" {
   180  			t.Fatalf("expected: compile-no-explicit-type; got: %s", ctx.Application.Name)
   181  		}
   182  		if ctx.Application.Type != "test-detection-merge" {
   183  			t.Fatalf("expected: test-detection-merge; got: %s", ctx.Application.Type)
   184  		}
   185  		return nil, nil
   186  	}
   187  	foundImpl := otto.TestFoundation(
   188  		t, foundation.Tuple{"consul", "aws", "simple"}, core)
   189  	ui := new(cli.MockUi)
   190  	detectors := []*detect.Detector{
   191  		&detect.Detector{
   192  			Type: "test-detection-merge",
   193  			File: []string{"test-file"},
   194  		},
   195  	}
   196  	c := &CompileCommand{
   197  		Meta: Meta{
   198  			CoreConfig: core,
   199  			Ui:         ui,
   200  		},
   201  		Detectors: detectors,
   202  	}
   203  
   204  	dir := fixtureDir("compile-detection")
   205  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   206  	defer testChdir(t, dir)()
   207  
   208  	args := []string{}
   209  	if code := c.Run(args); code != 0 {
   210  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   211  	}
   212  
   213  	if !infra.CompileCalled {
   214  		t.Fatal("Compile should be called")
   215  	}
   216  	if !appImpl.CompileCalled {
   217  		t.Fatal("Compile should be called")
   218  	}
   219  	if !foundImpl.CompileCalled {
   220  		t.Fatal("Foundation should be called")
   221  	}
   222  }
   223  
   224  func TestCompile_noAppFile(t *testing.T) {
   225  	core := otto.TestCoreConfig(t)
   226  	infra := otto.TestInfra(t, "aws", core)
   227  	appImpl := otto.TestApp(t, app.Tuple{"test", "aws", "simple"}, core)
   228  	appImpl.CompileFunc = func(ctx *app.Context) (*app.CompileResult, error) {
   229  		if ctx.Application == nil {
   230  			t.Fatal("application unexpectedly nil")
   231  		}
   232  		if ctx.Application.Name != "compile-no-appfile" {
   233  			t.Fatalf("expected: compile-no-appfile; got: %s", ctx.Application.Name)
   234  		}
   235  		return nil, nil
   236  	}
   237  	foundImpl := otto.TestFoundation(
   238  		t, foundation.Tuple{"consul", "aws", "simple"}, core)
   239  	ui := new(cli.MockUi)
   240  	detectors := []*detect.Detector{
   241  		&detect.Detector{
   242  			Type: "test",
   243  			File: []string{"test-file"},
   244  		},
   245  	}
   246  	c := &CompileCommand{
   247  		Meta: Meta{
   248  			CoreConfig: core,
   249  			Ui:         ui,
   250  		},
   251  		Detectors: detectors,
   252  	}
   253  
   254  	dir := fixtureDir("compile-no-appfile")
   255  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   256  	defer testChdir(t, dir)()
   257  
   258  	args := []string{}
   259  	if code := c.Run(args); code != 0 {
   260  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   261  	}
   262  
   263  	if !infra.CompileCalled {
   264  		t.Fatal("Compile should be called")
   265  	}
   266  	if !appImpl.CompileCalled {
   267  		t.Fatal("Compile should be called")
   268  	}
   269  	if !foundImpl.CompileCalled {
   270  		t.Fatal("Foundation should be called")
   271  	}
   272  }
   273  
   274  func TestCompile_pathFile(t *testing.T) {
   275  	ui := new(cli.MockUi)
   276  	c := &CompileCommand{
   277  		Meta: Meta{
   278  			CoreConfig: otto.TestCoreConfig(t),
   279  			Ui:         ui,
   280  		},
   281  	}
   282  
   283  	dir := fixtureDir("compile-file")
   284  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   285  	defer testChdir(t, dir)()
   286  
   287  	args := []string{"-appfile", "Appfile.other"}
   288  	if code := c.Run(args); code != 0 {
   289  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   290  	}
   291  }
   292  
   293  func TestCompile_pathDir(t *testing.T) {
   294  	ui := new(cli.MockUi)
   295  	c := &CompileCommand{
   296  		Meta: Meta{
   297  			CoreConfig: otto.TestCoreConfig(t),
   298  			Ui:         ui,
   299  		},
   300  	}
   301  
   302  	dir := fixtureDir("compile-dir")
   303  	defer os.Remove(filepath.Join(dir, "dir", ".ottoid"))
   304  	defer testChdir(t, dir)()
   305  
   306  	args := []string{"-appfile", "dir"}
   307  	if code := c.Run(args); code != 0 {
   308  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   309  	}
   310  }
   311  
   312  func TestCompile_altFile(t *testing.T) {
   313  	ui := new(cli.MockUi)
   314  	c := &CompileCommand{
   315  		Meta: Meta{
   316  			CoreConfig: otto.TestCoreConfig(t),
   317  			Ui:         ui,
   318  		},
   319  	}
   320  
   321  	dir := fixtureDir("compile-alt")
   322  	defer os.Remove(filepath.Join(dir, ".ottoid"))
   323  	defer testChdir(t, dir)()
   324  
   325  	args := []string{}
   326  	if code := c.Run(args); code != 0 {
   327  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
   328  	}
   329  }
   330  
   331  func testChdir(t *testing.T, dir string) func() {
   332  	wd, err := os.Getwd()
   333  	if err != nil {
   334  		t.Fatal(err)
   335  	}
   336  	if err := os.Chdir(dir); err != nil {
   337  		t.Fatal(err)
   338  	}
   339  	return func() {
   340  		if err := os.Chdir(wd); err != nil {
   341  			t.Fatal(err)
   342  		}
   343  	}
   344  }