github.com/exercism/v2-configlet@v3.9.2+incompatible/cmd/upgrade_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/exercism/configlet/ui"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type fakeCLI struct {
    12  	UpToDate      bool
    13  	UpgradeCalled bool
    14  }
    15  
    16  func (fc *fakeCLI) IsUpToDate() (bool, error) {
    17  	return fc.UpToDate, nil
    18  }
    19  
    20  func (fc *fakeCLI) Upgrade() error {
    21  	fc.UpgradeCalled = true
    22  	return nil
    23  }
    24  
    25  func TestUpgrade(t *testing.T) {
    26  	oldOut := ui.Out
    27  	ui.Out = ioutil.Discard
    28  	defer func() { ui.Out = oldOut }()
    29  
    30  	tests := []struct {
    31  		desc     string
    32  		upToDate bool
    33  		expected bool
    34  	}{
    35  		{
    36  			desc:     "upgrade should be called for an outdated CLI",
    37  			upToDate: false,
    38  			expected: true,
    39  		},
    40  		{
    41  			desc:     "upgrade should not be called for an already updated CLI",
    42  			upToDate: true,
    43  			expected: false,
    44  		},
    45  	}
    46  
    47  	for _, test := range tests {
    48  		fc := &fakeCLI{UpToDate: test.upToDate}
    49  
    50  		runUpdate(fc)
    51  		assert.Equal(t, test.expected, fc.UpgradeCalled, test.desc)
    52  	}
    53  }