-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tsh: ignore empty or non-existing config files (#11495)
If the newly created config.yaml didnt exist we would load the default values and continue the flow However, we were not resetting the `err`'s value and would have an ERROR message at the end and an invalid exit code Most of the commands would reset that variable to the output of the command's execution One of them was not: `tsh version` The version command has no return value, so the program would execute as expected until the last statement: `trace.Wrap(error)` which would re-use the `err` variable whose value is the result of the `loadConfig` method. We could either reset the `err`s value inside the `PrintVersion` switch case block or reset it right after we check for `IsNotFound`. We ended up picking the first option as it seems cleaner ``` # before $ make full > /dev/null; build/tsh version Teleport v10.0.0-dev git:v8.0.0-alpha.1-899-g335adf1f4 go1.18 ERROR: open /home/marco/.tsh/config/config.yaml: no such file or directory # after $ make full > /dev/null; build/tsh version Teleport v10.0.0-dev git:v8.0.0-alpha.1-899-g335adf1f4 go1.18 ```
- Loading branch information
1 parent
71f6153
commit 4b1c35a
Showing
3 changed files
with
60 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2022 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadConfigNonExistingFile(t *testing.T) { | ||
fullFilePath := "/tmp/doesntexist." + uuid.NewString() | ||
gotConfig, gotErr := loadConfig(fullFilePath) | ||
require.NoError(t, gotErr) | ||
require.Equal(t, &TshConfig{}, gotConfig) | ||
} | ||
|
||
func TestLoadConfigEmptyFile(t *testing.T) { | ||
file, err := os.CreateTemp("", "test-telelport") | ||
file.Write([]byte(" ")) | ||
require.NoError(t, err) | ||
defer os.Remove(file.Name()) | ||
|
||
gotConfig, gotErr := loadConfig(file.Name()) | ||
require.NoError(t, gotErr) | ||
require.Equal(t, &TshConfig{}, gotConfig) | ||
} |