Skip to content

Commit

Permalink
Move config file reading to another function for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Anderson committed Oct 6, 2022
1 parent e3a57f4 commit bc49b1e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 48 deletions.
49 changes: 1 addition & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
"path"
Expand Down Expand Up @@ -39,53 +37,8 @@ func main() {
Errors: make([]*Error, 0),
RWMutex: new(sync.RWMutex),
}
// 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)
}
read_config_file(cwd, *config_file, config)

// runner
if err := linter.Run(filesystem, config, *debug, false); err != nil {
Expand Down
60 changes: 60 additions & 0 deletions read_config_file.go
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)
}
}
27 changes: 27 additions & 0 deletions read_config_file_test.go
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")
}
}

0 comments on commit bc49b1e

Please sign in to comment.