github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/internal/cmdapi/cmdapi_test.go (about)

     1  // Copyright 2021-present The Atlas Authors. All rights reserved.
     2  // This source code is licensed under the Apache 2.0 license found
     3  // in the LICENSE file in the root directory of this source tree.
     4  
     5  package cmdapi
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"database/sql"
    11  	"fmt"
    12  	"os"
    13  	"os/exec"
    14  	"testing"
    15  
    16  	"github.com/iasthc/atlas/sql/sqlite"
    17  	"github.com/spf13/cobra"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestCLI_Version(t *testing.T) {
    22  	// Required to have a clean "stderr" while running first time.
    23  	tests := []struct {
    24  		name     string
    25  		cmd      *exec.Cmd
    26  		expected string
    27  	}{
    28  		{
    29  			name: "dev mode",
    30  			cmd: exec.Command("go", "run", "github.com/iasthc/atlas/cmd/atlas",
    31  				"version",
    32  			),
    33  			expected: "atlas version - development\nhttps://github.com/ariga/atlas/releases/latest\n",
    34  		},
    35  		{
    36  			name: "release",
    37  			cmd: exec.Command("go", "run",
    38  				"-ldflags",
    39  				"-X github.com/iasthc/atlas/cmd/atlas/internal/cmdapi.version=v1.2.3",
    40  				"github.com/iasthc/atlas/cmd/atlas",
    41  				"version",
    42  			),
    43  			expected: "atlas version v1.2.3\nhttps://github.com/ariga/atlas/releases/tag/v1.2.3\n",
    44  		},
    45  		{
    46  			name: "canary",
    47  			cmd: exec.Command("go", "run",
    48  				"-ldflags",
    49  				"-X github.com/iasthc/atlas/cmd/atlas/internal/cmdapi.version=v0.3.0-6539f2704b5d-canary",
    50  				"github.com/iasthc/atlas/cmd/atlas",
    51  				"version",
    52  			),
    53  			expected: "atlas version v0.3.0-6539f2704b5d-canary\nhttps://github.com/ariga/atlas/releases/latest\n",
    54  		},
    55  	}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			t.Setenv("ATLAS_NO_UPDATE_NOTIFIER", "true")
    59  			stdout := bytes.NewBuffer(nil)
    60  			tt.cmd.Stdout = stdout
    61  			tt.cmd.Stderr = os.Stderr
    62  			require.NoError(t, tt.cmd.Run())
    63  			require.Equal(t, tt.expected, stdout.String())
    64  		})
    65  	}
    66  }
    67  
    68  func runCmd(cmd *cobra.Command, args ...string) (string, error) {
    69  	var out bytes.Buffer
    70  	cmd.SetOut(&out)
    71  	cmd.SetErr(&out)
    72  	// Cobra checks for the args to equal nil and if so uses os.Args[1:].
    73  	// In tests, this leads to go tooling arguments being part of the command arguments.
    74  	if args == nil {
    75  		args = []string{}
    76  	}
    77  	cmd.SetArgs(args)
    78  	err := cmd.Execute()
    79  	return out.String(), err
    80  }
    81  
    82  // openSQLite creates a sqlite db, seeds it with the seed query and returns the url to it.
    83  func openSQLite(t *testing.T, seed string) string {
    84  	f, err := os.CreateTemp("", "sqlite.db")
    85  	require.NoError(t, err)
    86  	t.Cleanup(func() {
    87  		os.Remove(f.Name())
    88  	})
    89  	dsn := fmt.Sprintf("file:%s?cache=shared&_fk=1", f.Name())
    90  	db, err := sql.Open("sqlite3", dsn)
    91  	require.NoError(t, err)
    92  	t.Cleanup(func() {
    93  		db.Close()
    94  	})
    95  	drv, err := sqlite.Open(db)
    96  	require.NoError(t, err)
    97  	if len(seed) > 0 {
    98  		_, err := drv.ExecContext(context.Background(), seed)
    99  		require.NoError(t, err)
   100  	}
   101  	return fmt.Sprintf("sqlite://%s", dsn)
   102  }