-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move config file reading to another function for testing
- Loading branch information
Rob Anderson
committed
Oct 6, 2022
1 parent
e3a57f4
commit bc49b1e
Showing
3 changed files
with
88 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"gopkg.in/yaml.v3" | ||
"io" | ||
"log" | ||
"os" | ||
"path" | ||
) | ||
|
||
func read_config_file(cwd string, config_file string, config *Config) { | ||
|
||
// if config_file is default, see if there's an `.ls-lint.yaml` file instead | ||
if config_file == ".ls-lint.yml" { | ||
files, err := os.ReadDir(cwd) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, file := range files { | ||
match, match_err := path.Match(".ls-lint.y*ml", file.Name()) | ||
if match_err != nil { | ||
log.Fatal(match_err) | ||
} | ||
if match { | ||
config_file = file.Name() | ||
break | ||
} | ||
} | ||
} | ||
// open config file | ||
file, err := os.Open(config_file) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// close file | ||
defer func() { | ||
err = file.Close() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
// read file | ||
configBytes, err := io.ReadAll(file) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// to yaml | ||
err = yaml.Unmarshal(configBytes, &config) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,27 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestReadConfigFile(t *testing.T) { | ||
examples_config := &Config{ | ||
RWMutex: new(sync.RWMutex), | ||
} | ||
root_config := &Config { | ||
RWMutex: new(sync.RWMutex), | ||
} | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
read_config_file(cwd, "./examples/nuxt-nuxt-js/.ls-lint.yml", examples_config) | ||
read_config_file(cwd, ".ls-lint.yml", root_config) | ||
if reflect.DeepEqual(examples_config.getLs(), root_config.getLs()) { | ||
t.Errorf("Both configuration files have the same ls rules") | ||
} | ||
} |