gridscale / gscloud

The official command-line interface for the gridscale API
https://gridscale.io/
MIT License
16 stars 8 forks source link

Fixes a crash that happens when there is no account in the config #146

Closed JanLukasGithub closed 2 years ago

JanLukasGithub commented 2 years ago

This made it impossible to run without the config file

JanLukasGithub commented 2 years ago

I will add some tests...

nvthongswansea commented 2 years ago

@JanLukasGithub I think you could do this instead: Put this

if accountIndex == -1 { 
   return fmt.Errorf("account not found")
}

above the line ac = LoadEnvVariables(ac). Then in the test, you could do assert.NotNil(t, err), which makes more sense of "if you choose wrong account, there will be an error".

JanLukasGithub commented 2 years ago

There are commands you don't need an account for, like make-config and version, where you don't need/can't have an account

nvthongswansea commented 2 years ago

@JanLukasGithub btw, instead of separating the unit tests, combine these Test_SelectAccount, Test_SelectAccountSettings, and Test_EmptyConfig into one called Test_NewRuntime; and implement test cases. Using Test_NewRuntime tells you what function/method you are testing.

nvthongswansea commented 2 years ago

There are commands you don't need an account for, like make-config and version, where you don't need/can't have an account

In line 231, we do have

if len(conf.Accounts) > 0 && accountIndex == -1 {
        if !CommandWithoutConfig(os.Args) {
            return nil, fmt.Errorf("account '%s' does not exist", accountName)
        }
    }

just remove len(conf.Accounts) > 0, and do accountIndex == -1 && !CommandWithoutConfig(os.Args).

nvthongswansea commented 2 years ago

Ah, you want to to get rid of the case accountIndex == -1 and the command doesn't need the config. Sorry, misunderstanding.

JanLukasGithub commented 2 years ago

I think this is a bit better

JanLukasGithub commented 2 years ago

@JanLukasGithub btw, instead of separating the unit tests, combine these Test_SelectAccount, Test_SelectAccountSettings, and Test_EmptyConfig into one called Test_NewRuntime; and implement test cases. Using Test_NewRuntime tells you what function/method you are testing.

What I would now think of is doing a method Test_NewRuntime which calls those 4 other functions, or else you get 1 function that's far to big and not readable, but even one big function would be better than my approach I guess

JanLukasGithub commented 2 years ago

I think the problem is that the environment variables are loaded inside this function which makes it multi-purpose and hard to test properly as there would be a lot of really different test cases, like one test case has to test environment variables, while the others shouldn't have them set at all

nvthongswansea commented 2 years ago

@JanLukasGithub btw, instead of separating the unit tests, combine these Test_SelectAccount, Test_SelectAccountSettings, and Test_EmptyConfig into one called Test_NewRuntime; and implement test cases. Using Test_NewRuntime tells you what function/method you are testing.

What I would now think of is doing a method Test_NewRuntime which calls those 4 other functions, or else you get 1 function that's far to big and not readable, but even one big function would be better than my approach I guess

The truth is using test cases in a sinhle unit test function is more compact (less code) and easier to manage. You don't have to call those 4 functions either.

nvthongswansea commented 2 years ago

Other than the test, lgtm.

nvthongswansea commented 2 years ago

@JanLukasGithub Example for multiple test cases testing: https://github.com/gridscale/packer-plugin-gridscale/blob/main/builder/gridscale/builder_test.go

bkircher commented 2 years ago

Or maybe this helps: https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go

nvthongswansea commented 2 years ago

LGTM

bkircher commented 2 years ago

Hey @nvthongswansea if it's a LGTM, just merge it. No reason to keep these open 😄