github.com/editorconfig-checker/editorconfig-checker@v0.0.0-20231102090242-ddae3e68851e/pkg/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  const (
    13  	rootConfigFilePath        = "../../.ecrc"
    14  	configWithIgnoredDefaults = "../../testfiles/.ecrc"
    15  )
    16  
    17  func TestNewConfig(t *testing.T) {
    18  	actual, _ := NewConfig("abc")
    19  	var expected Config
    20  
    21  	if actual == &expected {
    22  		t.Errorf("expected %v, got %v", expected, actual)
    23  	}
    24  }
    25  
    26  func TestGetExcludesAsRegularExpression(t *testing.T) {
    27  	c, _ := NewConfig(configWithIgnoredDefaults)
    28  	err := c.Parse()
    29  	if err != nil {
    30  		t.Errorf("Should parse without an error, got: %v", err)
    31  	}
    32  
    33  	actual := c.GetExcludesAsRegularExpression()
    34  	expected := "testfiles"
    35  
    36  	if actual != expected {
    37  		t.Errorf("expected %s, got %s", expected, actual)
    38  	}
    39  
    40  	c.Exclude = append(c.Exclude, "stuff")
    41  
    42  	actual = c.GetExcludesAsRegularExpression()
    43  	expected = "testfiles|stuff"
    44  
    45  	if actual != expected {
    46  		t.Errorf("expected %s, got %s", expected, actual)
    47  	}
    48  
    49  	c, _ = NewConfig(rootConfigFilePath)
    50  	err = c.Parse()
    51  	if err != nil {
    52  		t.Errorf("Should parse without an error, got: %v", err)
    53  	}
    54  
    55  	actual = c.GetExcludesAsRegularExpression()
    56  	expected = `testfiles|testdata|^\.yarn/|^yarn\.lock$|^package-lock\.json$|^composer\.lock$|^Cargo\.lock$|^Gemfile\.lock$|^\.pnp\.cjs$|^\.pnp\.js$|^\.pnp\.loader\.mjs$|\.snap$|\.otf$|\.woff$|\.woff2$|\.eot$|\.ttf$|\.gif$|\.png$|\.jpg$|\.jpeg$|\.webp$|\.avif$|\.mp4$|\.wmv$|\.svg$|\.ico$|\.bak$|\.bin$|\.pdf$|\.zip$|\.gz$|\.tar$|\.7z$|\.bz2$|\.log$|\.patch$|\.css\.map$|\.js\.map$|min\.css$|min\.js$`
    57  
    58  	if actual != expected {
    59  		t.Errorf("expected %s, got %s", expected, actual)
    60  	}
    61  }
    62  
    63  func TestMerge(t *testing.T) {
    64  	c1, err := NewConfig("../../.ecrc")
    65  	if err != nil {
    66  		t.Errorf("Expected to create a config without errors, got %v", err)
    67  	}
    68  
    69  	err = c1.Parse()
    70  	if err != nil {
    71  		t.Errorf("Expected to parse a config without errors, got %v", err)
    72  	}
    73  
    74  	mergeConfig := Config{}
    75  	c1.Merge(mergeConfig)
    76  
    77  	c2, _ := NewConfig("../../.ecrc")
    78  	_ = c2.Parse()
    79  
    80  	if !reflect.DeepEqual(c1, c2) {
    81  		t.Errorf("Expected a parsed config and a parsed config merged with an empty config to be the same config, got %v and %v", c1, c2)
    82  	}
    83  
    84  	mergeConfig = Config{
    85  		ShowVersion:         true,
    86  		Version:             "2.7.2",
    87  		Help:                true,
    88  		DryRun:              true,
    89  		Path:                "some-other",
    90  		Verbose:             true,
    91  		Debug:               true,
    92  		NoColor:             true,
    93  		IgnoreDefaults:      true,
    94  		SpacesAftertabs:     true,
    95  		Exclude:             []string{"some-other"},
    96  		PassedFiles:         []string{"src"},
    97  		AllowedContentTypes: []string{"xml/"},
    98  		Disable: DisabledChecks{
    99  			TrimTrailingWhitespace: true,
   100  			EndOfLine:              true,
   101  			InsertFinalNewline:     true,
   102  			Indentation:            true,
   103  			IndentSize:             true,
   104  			MaxLineLength:          true,
   105  		},
   106  	}
   107  
   108  	c1.Merge(mergeConfig)
   109  
   110  	mergeConfig.AllowedContentTypes = []string{"text/", "application/octet-stream", "application/ecmascript", "application/json", "application/x-ndjson", "application/xml", "+json", "+xml", "xml/"}
   111  	mergeConfig.Exclude = []string{"testfiles", "testdata", "some-other"}
   112  
   113  	expected := mergeConfig
   114  	expected.Logger.Verbosee = true
   115  	expected.Logger.Debugg = true
   116  	expected.Logger.NoColor = true
   117  	expected.EditorconfigConfig = c1.EditorconfigConfig
   118  
   119  	if !reflect.DeepEqual(c1, &expected) {
   120  		t.Errorf("%#v", &expected)
   121  		t.Errorf("%#v", c1)
   122  		t.Errorf("Expected, got %#v and %#v", c1, &expected)
   123  	}
   124  
   125  	config := Config{Path: "./.ecrc"}
   126  	err = config.Parse()
   127  
   128  	if err == nil {
   129  		t.Errorf("Expected an error to happen when parsing an unexisting file, got %v", err)
   130  	}
   131  
   132  	config = Config{Path: "./../../testfiles/.malformed.ecrc"}
   133  	err = config.Parse()
   134  
   135  	if err == nil {
   136  		t.Errorf("Expected an error to happen when parsing an unexisting file, got %v", err)
   137  	}
   138  }
   139  
   140  func TestParse(t *testing.T) {
   141  	c, _ := NewConfig("../../testfiles/.ecrc")
   142  	_ = c.Parse()
   143  
   144  	if c.Verbose != true ||
   145  		c.Debug != true ||
   146  		c.IgnoreDefaults != true ||
   147  		!reflect.DeepEqual(c.Exclude, []string{"testfiles"}) ||
   148  		!reflect.DeepEqual(c.AllowedContentTypes, []string{"text/", "application/octet-stream", "application/ecmascript", "application/json", "application/x-ndjson", "application/xml", "+json", "+xml", "hey"}) ||
   149  		c.SpacesAftertabs != true ||
   150  		c.Disable.EndOfLine != false ||
   151  		c.Disable.TrimTrailingWhitespace != false ||
   152  		c.Disable.InsertFinalNewline != false ||
   153  		c.Disable.Indentation != false {
   154  		t.Error(c.AllowedContentTypes)
   155  		t.Errorf("Expected config to have values from test file, got %v", c)
   156  	}
   157  }
   158  
   159  func TestSave(t *testing.T) {
   160  	dir, _ := ioutil.TempDir("", "example")
   161  	defer os.RemoveAll(dir)
   162  	configFile := filepath.Join(dir, "config")
   163  	c, _ := NewConfig(configFile)
   164  	if c.Save("VERSION") != nil {
   165  		t.Error("Should create the config")
   166  	}
   167  
   168  	if c.Save("VERSION") == nil {
   169  		t.Error("Should produce an error")
   170  	}
   171  }
   172  
   173  func TestGetAsString(t *testing.T) {
   174  	c, _ := NewConfig("../../.ecrc")
   175  	_ = c.Parse()
   176  
   177  	actual := c.GetAsString()
   178  	expected := "Config: {ShowVersion:false Help:false DryRun:false Path:../../.ecrc Version:2.7.2 Verbose:false Debug:false IgnoreDefaults:false SpacesAftertabs:false NoColor:false Exclude:[testfiles testdata] AllowedContentTypes:[text/ application/octet-stream application/ecmascript application/json application/x-ndjson application/xml +json +xml] PassedFiles:[] Disable:{EndOfLine:false Indentation:false InsertFinalNewline:false TrimTrailingWhitespace:false IndentSize:false MaxLineLength:false} Logger:{Verbosee:false Debugg:false NoColor:false} EditorconfigConfig:0x"
   179  
   180  	if !strings.HasPrefix(actual, expected) {
   181  		t.Errorf("Expected: %v, got: %v ", expected, actual)
   182  	}
   183  }