github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/test/cli/root_cmd_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/sergi/go-diff/diffmatchpatch"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestRootCmdAliasesToPackagesSubcommand(t *testing.T) {
    13  	request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
    14  
    15  	tests := []struct {
    16  		name       string
    17  		env        map[string]string
    18  		assertions []traitAssertion
    19  	}{
    20  		{
    21  			name: "go-case",
    22  			assertions: []traitAssertion{
    23  				assertTableReport,
    24  				assertSuccessfulReturnCode,
    25  			},
    26  		},
    27  		{
    28  			name: "respond-to-output-binding",
    29  			env: map[string]string{
    30  				"BUILDX_OUTPUT": "text",
    31  			},
    32  			assertions: []traitAssertion{
    33  				assertInOutput("[Image]"),
    34  				assertSuccessfulReturnCode,
    35  			},
    36  		},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		t.Run(test.name, func(t *testing.T) {
    41  			aliasCmd, aliasStdout, aliasStderr := runBuildx(t, test.env, request)
    42  			for _, traitFn := range test.assertions {
    43  				traitFn(t, aliasStdout, aliasStderr, aliasCmd.ProcessState.ExitCode())
    44  			}
    45  
    46  			pkgCmd, pkgsStdout, pkgsStderr := runBuildx(t, test.env, "packages", request)
    47  			for _, traitFn := range test.assertions {
    48  				traitFn(t, pkgsStdout, pkgsStderr, pkgCmd.ProcessState.ExitCode())
    49  			}
    50  
    51  			if aliasStdout != pkgsStdout {
    52  				t.Errorf("packages and root command should have same report output but do not!")
    53  				dmp := diffmatchpatch.New()
    54  				diffs := dmp.DiffMain(aliasStdout, pkgsStdout, true)
    55  				t.Error(dmp.DiffPrettyText(diffs))
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestPersistentFlags(t *testing.T) {
    62  	request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
    63  
    64  	tests := []struct {
    65  		name       string
    66  		args       []string
    67  		env        map[string]string
    68  		assertions []traitAssertion
    69  	}{
    70  		{
    71  			name: "quiet-flag",
    72  			// note: the root command will always show the deprecation warning, so the packages command is used instead
    73  			args: []string{"packages", "-q", request},
    74  			assertions: []traitAssertion{
    75  				func(tb testing.TB, stdout, stderr string, rc int) {
    76  					// ensure there is no status
    77  					if len(stderr) != 0 {
    78  						tb.Errorf("should have seen no stderr output, got %d bytes", len(stderr))
    79  					}
    80  					// ensure there is still a report
    81  					if len(stdout) == 0 {
    82  						tb.Errorf("should have seen a report on stdout, got nothing")
    83  					}
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "info-log-flag",
    89  			args: []string{"-v", request},
    90  			assertions: []traitAssertion{
    91  				assertLoggingLevel("info"),
    92  				assertSuccessfulReturnCode,
    93  			},
    94  		},
    95  		{
    96  			name: "debug-log-flag",
    97  			args: []string{"-vv", request},
    98  			assertions: []traitAssertion{
    99  				assertLoggingLevel("debug"),
   100  				assertSuccessfulReturnCode,
   101  			},
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			cmd, stdout, stderr := runBuildx(t, test.env, test.args...)
   108  			for _, traitFn := range test.assertions {
   109  				traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
   110  			}
   111  			logOutputOnFailure(t, cmd, stdout, stderr)
   112  		})
   113  	}
   114  }
   115  
   116  func TestLogFile(t *testing.T) {
   117  	request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
   118  
   119  	envLogFile := filepath.Join(os.TempDir(), "a-pretty-log-file.log")
   120  	tests := []struct {
   121  		name       string
   122  		args       []string
   123  		env        map[string]string
   124  		assertions []traitAssertion
   125  		cleanup    func()
   126  	}{
   127  		{
   128  			name: "env-var-log-file-name",
   129  			args: []string{"-vv", request},
   130  			env:  map[string]string{"BUILDX_LOG_FILE": envLogFile},
   131  			assertions: []traitAssertion{
   132  				func(tb testing.TB, stdout, stderr string, rc int) {
   133  					tb.Helper()
   134  					_, err := os.Stat(envLogFile)
   135  					assert.NoError(t, err)
   136  				},
   137  			},
   138  			cleanup: func() { assert.NoError(t, os.Remove(envLogFile)) },
   139  		},
   140  	}
   141  	for _, test := range tests {
   142  		t.Run(test.name, func(t *testing.T) {
   143  			t.Cleanup(test.cleanup)
   144  
   145  			cmd, stdout, stderr := runBuildx(t, test.env, test.args...)
   146  			for _, traitFn := range test.assertions {
   147  				traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
   148  			}
   149  			logOutputOnFailure(t, cmd, stdout, stderr)
   150  		})
   151  	}
   152  }