Error out if atlantis.yml is used as config file

This commit is contained in:
Max Curran
2019-10-26 22:05:27 +01:00
parent 6ade4ba1c7
commit c2934b4b49
2 changed files with 22 additions and 4 deletions

View File

@@ -27,7 +27,14 @@ type ParserValidator struct{}
// for the repo at absRepoDir.
// Returns an error if for some reason it can't read that directory.
func (p *ParserValidator) HasRepoCfg(absRepoDir string) (bool, error) {
_, err := os.Stat(p.repoCfgPath(absRepoDir))
// Checks for a config file with an invalid extension (atlantis.yml)
const invalidExtensionFilename = "atlantis.yml"
_, err := os.Stat(p.repoCfgPath(absRepoDir, invalidExtensionFilename))
if err == nil {
return false, errors.Errorf("found %q as config file; rename using the .yaml extension - %q", invalidExtensionFilename, AtlantisYAMLFilename)
}
_, err = os.Stat(p.repoCfgPath(absRepoDir, AtlantisYAMLFilename))
if os.IsNotExist(err) {
return false, nil
}
@@ -38,7 +45,7 @@ func (p *ParserValidator) HasRepoCfg(absRepoDir string) (bool, error) {
// repo at absRepoDir.
// If there was no config file, it will return an os.IsNotExist(error).
func (p *ParserValidator) ParseRepoCfg(absRepoDir string, globalCfg valid.GlobalCfg, repoID string) (valid.RepoCfg, error) {
configFile := p.repoCfgPath(absRepoDir)
configFile := p.repoCfgPath(absRepoDir, AtlantisYAMLFilename)
configData, err := ioutil.ReadFile(configFile) // nolint: gosec
if err != nil {
@@ -122,8 +129,8 @@ func (p *ParserValidator) validateRawGlobalCfg(rawCfg raw.GlobalCfg, defaultCfg
return validCfg, nil
}
func (p *ParserValidator) repoCfgPath(repoDir string) string {
return filepath.Join(repoDir, AtlantisYAMLFilename)
func (p *ParserValidator) repoCfgPath(repoDir, cfgFilename string) string {
return filepath.Join(repoDir, cfgFilename)
}
func (p *ParserValidator) validateProjectNames(config valid.RepoCfg) error {

View File

@@ -33,6 +33,17 @@ func TestHasRepoCfg_FileDoesNotExist(t *testing.T) {
Equals(t, false, exists)
}
func TestHasRepoCfg_InvalidFileExtension(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
_, err := os.Create(filepath.Join(tmpDir, "atlantis.yml"))
Ok(t, err)
r := yaml.ParserValidator{}
_, err = r.HasRepoCfg(tmpDir)
ErrContains(t, "found \"atlantis.yml\" as config file; rename using the .yaml extension - \"atlantis.yaml\"", err)
}
func TestParseRepoCfg_DirDoesNotExist(t *testing.T) {
r := yaml.ParserValidator{}
_, err := r.ParseRepoCfg("/not/exist", globalCfg, "")