Use filepath instead of path (#1247)

## Description

`path` is not cross platform as it hardcodes / as the separator. Also add a lint rule for this.

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* fixes https://github.com/alcionai/corso/issues/1236

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Abin Simon 2022-10-21 04:27:30 +05:30 committed by GitHub
parent 8906fbea17
commit 1cad17bfc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 16 deletions

View File

@ -25,6 +25,9 @@ linters-settings:
# Don't allow creating contexts without logging in tests. Use an ignore
# lower down to ensure usages of this outside of tests aren't reported.
- 'context\.(Background|TODO)(# tests should use tester\.NewContext )?'
# Don't allow use of pathas it hardcodes separator to `/`.
# Use filepath instead.
- '\bpath\.(Ext|Base|Dir|Join)'
lll:
line-length: 120
revive:

View File

@ -3,7 +3,6 @@ package config
import (
"context"
"os"
"path"
"path/filepath"
"strings"
@ -118,14 +117,14 @@ func initWithViper(vpr *viper.Viper, configFP string) error {
// We also configure the path, type and filename
// because `vpr.SafeWriteConfig` needs these set to
// work correctly (it does not use the configured file)
vpr.AddConfigPath(path.Dir(configFP))
vpr.AddConfigPath(filepath.Dir(configFP))
ext := path.Ext(configFP)
ext := filepath.Ext(configFP)
if len(ext) == 0 {
return errors.New("config file requires an extension e.g. `toml`")
}
fileName := path.Base(configFP)
fileName := filepath.Base(configFP)
fileName = strings.TrimSuffix(fileName, ext)
vpr.SetConfigType(strings.TrimPrefix(ext, "."))
vpr.SetConfigName(fileName)

View File

@ -4,7 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"github.com/spf13/viper"
@ -50,7 +50,7 @@ func (suite *ConfigSuite) TestReadRepoConfigBasic() {
// Generate test config file
testConfigData := fmt.Sprintf(configFileTemplate, b, tID)
testConfigFilePath := path.Join(t.TempDir(), "corso.toml")
testConfigFilePath := filepath.Join(t.TempDir(), "corso.toml")
err := ioutil.WriteFile(testConfigFilePath, []byte(testConfigData), 0o700)
require.NoError(t, err)
@ -81,7 +81,7 @@ func (suite *ConfigSuite) TestWriteReadConfig() {
)
// Configure viper to read test config file
testConfigFilePath := path.Join(t.TempDir(), "corso.toml")
testConfigFilePath := filepath.Join(t.TempDir(), "corso.toml")
require.NoError(t, initWithViper(vpr, testConfigFilePath), "initializing repo config")
s3Cfg := storage.S3Config{Bucket: bkt}
@ -111,7 +111,7 @@ func (suite *ConfigSuite) TestMustMatchConfig() {
)
// Configure viper to read test config file
testConfigFilePath := path.Join(t.TempDir(), "corso.toml")
testConfigFilePath := filepath.Join(t.TempDir(), "corso.toml")
require.NoError(t, initWithViper(vpr, testConfigFilePath), "initializing repo config")
s3Cfg := storage.S3Config{Bucket: bkt}
@ -213,7 +213,7 @@ func (suite *ConfigIntegrationSuite) TestGetStorageAndAccount() {
)
// Configure viper to read test config file
testConfigFilePath := path.Join(t.TempDir(), "corso.toml")
testConfigFilePath := filepath.Join(t.TempDir(), "corso.toml")
require.NoError(t, initWithViper(vpr, testConfigFilePath), "initializing repo config")
s3Cfg := storage.S3Config{

View File

@ -24,6 +24,8 @@ func encodeElements(elements ...string) []string {
// if they were a path. The elements are joined with the separator in the golang
// path package.
func encodeAsPath(elements ...string) string {
// Needs `/` to be used a separator here
//nolint:forbidigo
return path.Join(encodeElements(elements...)...)
}

View File

@ -2,7 +2,7 @@ package tester
import (
"os"
"path"
"path/filepath"
"strings"
"testing"
@ -59,16 +59,16 @@ func NewTestViper() (*viper.Viper, error) {
}
// Or use a custom file location
ext := path.Ext(configFilePath)
ext := filepath.Ext(configFilePath)
if len(ext) == 0 {
return nil, errors.New("corso_test requires an extension")
}
vpr.SetConfigFile(configFilePath)
vpr.AddConfigPath(path.Dir(configFilePath))
vpr.AddConfigPath(filepath.Dir(configFilePath))
vpr.SetConfigType(ext[1:])
fileName := path.Base(configFilePath)
fileName := filepath.Base(configFilePath)
fileName = strings.TrimSuffix(fileName, ext)
vpr.SetConfigName(fileName)
@ -139,19 +139,19 @@ func MakeTempTestConfigClone(t *testing.T, overrides map[string]string) (*viper.
return nil, "", err
}
fName := path.Base(os.Getenv(EnvCorsoTestConfigFilePath))
fName := filepath.Base(os.Getenv(EnvCorsoTestConfigFilePath))
if len(fName) == 0 || fName == "." || fName == "/" {
fName = ".corso_test.toml"
}
tDir := t.TempDir()
tDirFp := path.Join(tDir, fName)
tDirFp := filepath.Join(tDir, fName)
if _, err := os.Create(tDirFp); err != nil {
return nil, "", err
}
ext := path.Ext(fName)
ext := filepath.Ext(fName)
vpr := viper.New()
vpr.SetConfigFile(tDirFp)
vpr.AddConfigPath(tDir)