github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/project/project_test.go (about)

     1  package project_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/tooploox/oya/pkg/project"
     9  	"github.com/tooploox/oya/pkg/template"
    10  	tu "github.com/tooploox/oya/testutil"
    11  )
    12  
    13  var (
    14  	noArgs  []string
    15  	noFlags map[string]string
    16  	noScope template.Scope
    17  )
    18  
    19  func TestProject_Detect_NoOya(t *testing.T) {
    20  	workDir := "./fixtures/empty_project"
    21  	installDir := "" // Unused
    22  	_, err := project.Detect(workDir, installDir)
    23  	tu.AssertErr(t, err, "Expected error trying to detect Oya project in empty dir")
    24  }
    25  
    26  func TestProject_Detect_InRootDir(t *testing.T) {
    27  	workDir := "./fixtures/project"
    28  	installDir := "" // Unused
    29  	_, err := project.Detect(workDir, installDir)
    30  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    31  }
    32  
    33  func TestProject_Detect_InSubDir(t *testing.T) {
    34  	workDir := "./fixtures/project/subdir"
    35  	installDir := "" // Unused
    36  	_, err := project.Detect(workDir, installDir)
    37  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    38  }
    39  
    40  func TestProject_Detect_InEmptySubDir(t *testing.T) {
    41  	workDir := "./fixtures/project/empty_subdir"
    42  	installDir := "" // Unused
    43  	_, err := project.Detect(workDir, installDir)
    44  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    45  }
    46  
    47  func TestProject_Run_NoTask(t *testing.T) {
    48  	workDir := "./fixtures/project"
    49  	installDir := "" // Unused
    50  	project, err := project.Detect(workDir, installDir)
    51  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    52  	err = project.Run(workDir, "noSuchTask", false, false, nil, noScope, ioutil.Discard, ioutil.Discard)
    53  	tu.AssertErr(t, err, "Expected error when trying to run without matching task")
    54  }
    55  func TestProject_Run_NoTaskRecurse(t *testing.T) {
    56  	workDir := "./fixtures/project"
    57  	installDir := "" // Unused
    58  	project, err := project.Detect(workDir, installDir)
    59  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    60  	err = project.Run(workDir, "noSuchTask", true, false, nil, noScope, ioutil.Discard, ioutil.Discard)
    61  	tu.AssertErr(t, err, "Expected error when trying to run without matching task")
    62  }
    63  
    64  func TestProject_Run_NoChanges(t *testing.T) {
    65  	workDir := "./fixtures/empty_changeset_project"
    66  	installDir := "" // Unused
    67  	project, err := project.Detect(workDir, installDir)
    68  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    69  	stdout := new(bytes.Buffer)
    70  	stderr := new(bytes.Buffer)
    71  	err = project.Run(workDir, "build", false, true, nil, noScope, stdout, stderr)
    72  	tu.AssertNoErr(t, err, "Expected no error running with empty changeset")
    73  	tu.AssertEqual(t, 0, len(stdout.String()))
    74  	tu.AssertEqual(t, 0, len(stderr.String()))
    75  }
    76  
    77  func TestProject_Run_NoChangesRecurse(t *testing.T) {
    78  	workDir := "./fixtures/empty_changeset_project"
    79  	installDir := "" // Unused
    80  	project, err := project.Detect(workDir, installDir)
    81  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    82  	stdout := new(bytes.Buffer)
    83  	stderr := new(bytes.Buffer)
    84  	err = project.Run(workDir, "build", true, true, nil, noScope, stdout, stderr)
    85  	tu.AssertNoErr(t, err, "Expected no error running with empty changeset")
    86  	tu.AssertEqual(t, 0, len(stdout.String()))
    87  	tu.AssertEqual(t, 0, len(stderr.String()))
    88  }
    89  
    90  func TestProject_Run_WithChanges(t *testing.T) {
    91  	workDir := "./fixtures/project"
    92  	installDir := "" // Unused
    93  	project, err := project.Detect(workDir, installDir)
    94  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
    95  	stdout := new(bytes.Buffer)
    96  	stderr := new(bytes.Buffer)
    97  	err = project.Run(workDir, "build", false, true, nil, template.Scope{"Args": noArgs, "Flags": noFlags}, stdout, stderr)
    98  	tu.AssertNoErr(t, err, "Expected no error running non-empty changeset")
    99  	tu.AssertEqual(t, "build run", stdout.String())
   100  }
   101  
   102  func TestProject_Run_WithChangesRecurse(t *testing.T) {
   103  	workDir := "./fixtures/project"
   104  	installDir := "" // Unused
   105  	project, err := project.Detect(workDir, installDir)
   106  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
   107  	stdout := new(bytes.Buffer)
   108  	stderr := new(bytes.Buffer)
   109  	err = project.Run(workDir, "build", true, true, nil, template.Scope{"Args": noArgs, "Flags": noFlags}, stdout, stderr)
   110  	tu.AssertNoErr(t, err, "Expected no error running non-empty changeset")
   111  	tu.AssertEqual(t, "build run", stdout.String())
   112  }
   113  
   114  func TestProject_Run_WithArgs(t *testing.T) {
   115  	workDir := "./fixtures/project"
   116  	installDir := "" // Unused
   117  	project, err := project.Detect(workDir, installDir)
   118  	tu.AssertNoErr(t, err, "Expected no error trying to detect Oya project in its root dir")
   119  	stdout := new(bytes.Buffer)
   120  	stderr := new(bytes.Buffer)
   121  	err = project.Run(workDir, "build", false, true, []string{"arg1", "arg2", "--flag1", "--flag2"},
   122  		template.Scope{
   123  			"Args": []string{"arg1", "arg2"},
   124  			"Flags": map[string]string{
   125  				"flag1": "flag1",
   126  				"flag2": "flag2",
   127  			},
   128  		},
   129  		stdout, stderr)
   130  	tu.AssertNoErr(t, err, "Expected no error running non-empty changeset")
   131  	tu.AssertEqual(t, "build run\nAll args: arg1 arg2 --flag1 --flag2\nArgs: arg1 arg2\nFlags: flag1 flag2", stdout.String())
   132  }