|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | +) |
| 9 | + |
| 10 | +// Define the suite, and absorb the built-in basic suite |
| 11 | +// functionality from testify - including a T() method which |
| 12 | +// returns the current testing context |
| 13 | +type GetRepoCommandTestSuite struct { |
| 14 | + suite.Suite |
| 15 | + defaultKosliArguments string |
| 16 | + acmeOrgKosliArguments string |
| 17 | +} |
| 18 | + |
| 19 | +func (suite *GetRepoCommandTestSuite) SetupTest() { |
| 20 | + global = &GlobalOpts{ |
| 21 | + ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY", |
| 22 | + Org: "docs-cmd-test-user", |
| 23 | + Host: "http://localhost:8001", |
| 24 | + } |
| 25 | + suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken) |
| 26 | + |
| 27 | + global.Org = "iu-org-shared" |
| 28 | + global.ApiToken = "qM9u2_grv6pJLbACwsMMMT5LIQy82tQj2k1zjZnlXti1smnFaGwCKW4jzk0La7ae9RrSYvEwCXSsXknD6YZqd-onLaaIUUKtEn6-B6yh53vWIe9EC5u85FCbKZjFbaicp_d0Me0Zcqq_KcCgrAZRX9xggl_pBb2oaCsNdllqNjk" |
| 29 | + suite.acmeOrgKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken) |
| 30 | + CreateFlowWithTemplate("get-repo", "testdata/valid_template.yml", suite.T()) |
| 31 | + SetEnvVars(map[string]string{ |
| 32 | + "GITHUB_RUN_NUMBER": "1234", |
| 33 | + "GITHUB_SERVER_URL": "https://github.com", |
| 34 | + "GITHUB_REPOSITORY": "kosli-dev/cli", |
| 35 | + "GITHUB_REPOSITORY_ID": "1234567890", |
| 36 | + }, suite.T()) |
| 37 | + BeginTrail("trail-name", "get-repo", "", suite.T()) |
| 38 | +} |
| 39 | + |
| 40 | +func (suite *GetRepoCommandTestSuite) TearDownTest() { |
| 41 | + UnSetEnvVars(map[string]string{ |
| 42 | + "GITHUB_RUN_NUMBER": "", |
| 43 | + "GITHUB_SERVER_URL": "", |
| 44 | + "GITHUB_REPOSITORY": "", |
| 45 | + "GITHUB_REPOSITORY_ID": "", |
| 46 | + }, suite.T()) |
| 47 | +} |
| 48 | + |
| 49 | +func (suite *GetRepoCommandTestSuite) TestGetRepoCmd() { |
| 50 | + tests := []cmdTestCase{ |
| 51 | + { |
| 52 | + name: "01-getting a non-existing repo returns not-found message", |
| 53 | + cmd: fmt.Sprintf(`get repo non-existing/repo %s`, suite.defaultKosliArguments), |
| 54 | + golden: "Repo was not found.\n", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "02-getting an existing repo works", |
| 58 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli %s`, suite.acmeOrgKosliArguments), |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "03-getting an existing repo with --output json works", |
| 62 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli --output json %s`, suite.acmeOrgKosliArguments), |
| 63 | + goldenJson: []jsonCheck{{"_embedded.repos", "non-empty"}}, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "04-getting an existing repo with matching --provider works", |
| 67 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli --provider github %s`, suite.acmeOrgKosliArguments), |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "05-getting an existing repo with matching --provider and --output json works", |
| 71 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli --provider github --output json %s`, suite.acmeOrgKosliArguments), |
| 72 | + goldenJson: []jsonCheck{{"_embedded.repos", "non-empty"}}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "06-getting a repo with a non-matching --provider returns not-found message", |
| 76 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli --provider gitlab %s`, suite.acmeOrgKosliArguments), |
| 77 | + golden: "Repo was not found.\n", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "07-getting a repo with a non-matching --repo-id returns not-found message", |
| 81 | + cmd: fmt.Sprintf(`get repo kosli-dev/cli --repo-id non-existing-id %s`, suite.acmeOrgKosliArguments), |
| 82 | + golden: "Repo was not found.\n", |
| 83 | + }, |
| 84 | + { |
| 85 | + wantError: true, |
| 86 | + name: "08-providing no argument fails", |
| 87 | + cmd: fmt.Sprintf(`get repo %s`, suite.defaultKosliArguments), |
| 88 | + golden: "Error: accepts 1 arg(s), received 0\n", |
| 89 | + }, |
| 90 | + { |
| 91 | + wantError: true, |
| 92 | + name: "09-providing more than one argument fails", |
| 93 | + cmd: fmt.Sprintf(`get repo foo bar %s`, suite.defaultKosliArguments), |
| 94 | + golden: "Error: accepts 1 arg(s), received 2\n", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + runTestCmd(suite.T(), tests) |
| 99 | +} |
| 100 | + |
| 101 | +// In order for 'go test' to run this suite, we need to create |
| 102 | +// a normal test function and pass our suite to suite.Run |
| 103 | +func TestGetRepoCommandTestSuite(t *testing.T) { |
| 104 | + suite.Run(t, new(GetRepoCommandTestSuite)) |
| 105 | +} |
0 commit comments