github.com/wawandco/oxplugins@v0.7.11/tools/yarn/builder_test.go (about)

     1  package yarn
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  // TestBuildCmd attempts to run yarn install if it finds yarn.lock
    11  func TestBuildCmd(t *testing.T) {
    12  	err := os.Chdir(t.TempDir())
    13  	if err != nil {
    14  		t.Error(err)
    15  	}
    16  
    17  	tcases := []struct {
    18  		commandNil  bool
    19  		failMessage string
    20  		beforeFn    func()
    21  	}{
    22  		{
    23  			commandNil:  false,
    24  			failMessage: "command should not be nil",
    25  			beforeFn: func() {
    26  				err := ioutil.WriteFile("yarn.lock", []byte{}, 0600)
    27  				if err != nil {
    28  					t.Error(err)
    29  				}
    30  			},
    31  		},
    32  
    33  		{
    34  			commandNil:  true,
    35  			failMessage: "command should be nil",
    36  			beforeFn:    func() {},
    37  		},
    38  	}
    39  
    40  	p := &Plugin{}
    41  	for _, tcase := range tcases {
    42  		err := os.Remove("yarn.lock")
    43  		if err != nil && !os.IsNotExist(err) {
    44  			t.Error(err)
    45  			break
    46  		}
    47  
    48  		tcase.beforeFn()
    49  
    50  		c := p.buildCmd(context.Background())
    51  		cond := tcase.commandNil && c != nil
    52  		cond = cond || !tcase.commandNil && c == nil
    53  
    54  		if cond {
    55  			t.Error(tcase.failMessage)
    56  		}
    57  	}
    58  }