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

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/editorconfig-checker/editorconfig-checker/pkg/config"
     7  )
     8  
     9  func TestProcessValidation(t *testing.T) {
    10  	configuration := config.Config{
    11  		Verbose: true,
    12  	}
    13  
    14  	processValidationResult := ProcessValidation([]string{"./../../cmd/editorconfig-checker/main.go"}, configuration)
    15  	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
    16  		t.Error("Should not have errors when validating main.go, got", processValidationResult)
    17  	}
    18  
    19  	processValidationResult = ProcessValidation([]string{"./../../testfiles/disabled-file.ext"}, configuration)
    20  	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
    21  		t.Error("Disabled file should have no errors, got", processValidationResult)
    22  	}
    23  
    24  	processValidationResult = ProcessValidation([]string{"./../../testfiles/empty-file.txt"}, configuration)
    25  	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
    26  		t.Error("Empty file should have no errors, got", processValidationResult)
    27  	}
    28  
    29  	processValidationResult = ProcessValidation([]string{"./../../testfiles/wrong-file.txt"}, configuration)
    30  	if (len(processValidationResult) > 1) || (len(processValidationResult[0].Errors) != 1) {
    31  		t.Error("Wrong file should have errors, got", processValidationResult)
    32  	}
    33  }
    34  
    35  func TestValidateFile(t *testing.T) {
    36  	configuration := config.Config{Verbose: true}
    37  
    38  	result := ValidateFile("./../../cmd/editorconfig-checker/main.go", configuration)
    39  	if len(result) != 0 {
    40  		t.Error("Should not have errors when validating main.go, got", result)
    41  	}
    42  
    43  	result = ValidateFile("./../../testfiles/wrong-file.txt", configuration)
    44  	if len(result) != 1 {
    45  		t.Error("Should have errors when validating file with one error, got", result)
    46  	}
    47  
    48  	configuration.Disable.Indentation = true
    49  	result = ValidateFile("./../../testfiles/wrong-file.txt", configuration)
    50  	if len(result) != 0 {
    51  		t.Error("Should have no errors, got", result)
    52  	}
    53  
    54  	configuration = config.Config{SpacesAftertabs: true}
    55  	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", configuration)
    56  	if len(result) != 0 {
    57  		t.Error("Should have no errors when validating valid file, got", result)
    58  	}
    59  
    60  	configuration = config.Config{SpacesAftertabs: false}
    61  	result = ValidateFile("./../../testfiles/zero-indent.txt", configuration)
    62  	if len(result) != 0 {
    63  		t.Error("Should have no errors when validating valid file, got", result)
    64  	}
    65  
    66  	result = ValidateFile("./../../testfiles/disabled-line.txt", configuration)
    67  	if len(result) != 0 {
    68  		t.Error("Should have no errors when validating valid file, got", result)
    69  	}
    70  
    71  	result = ValidateFile("./../../testfiles/disabled-block.txt", configuration)
    72  	if len(result) != 0 {
    73  		t.Error("Should have no errors when validating valid file, got", result)
    74  	}
    75  
    76  	result = ValidateFile("./../../testfiles/disabled-block-with-error.txt", configuration)
    77  	if len(result) != 1 {
    78  		t.Error("Should have one error, got", result)
    79  	}
    80  
    81  	configuration = config.Config{SpacesAftertabs: false}
    82  	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", configuration)
    83  	if len(result) != 1 {
    84  		t.Error("Should have one error, got", result)
    85  	}
    86  
    87  	configuration = config.Config{Verbose: true}
    88  	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", configuration)
    89  	if len(result) != 1 {
    90  		t.Error("Should have one error, got", result)
    91  	}
    92  
    93  	configuration = config.Config{Verbose: true}
    94  	configuration.Disable.TrimTrailingWhitespace = true
    95  	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", configuration)
    96  	if len(result) != 0 {
    97  		t.Error("Should have no error, got", result)
    98  	}
    99  
   100  	configuration = config.Config{Verbose: true}
   101  	result = ValidateFile("./../../testfiles/final-newline-missing.txt", configuration)
   102  	if len(result) != 1 {
   103  		t.Error("Should have one error, got", result)
   104  	}
   105  
   106  	configuration = config.Config{Verbose: true}
   107  	configuration.Disable.InsertFinalNewline = true
   108  	result = ValidateFile("./../../testfiles/final-newline-missing.txt", configuration)
   109  	if len(result) != 0 {
   110  		t.Error("Should have no error, got", result)
   111  	}
   112  
   113  	configuration = config.Config{Verbose: true}
   114  	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", configuration)
   115  	if len(result) == 0 {
   116  		t.Error("Should have one error, got", result)
   117  	}
   118  
   119  	configuration = config.Config{Verbose: true}
   120  	configuration.Disable.EndOfLine = true
   121  	configuration.Disable.InsertFinalNewline = true
   122  	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", configuration)
   123  	if len(result) != 0 {
   124  		t.Error("Should have no error, got", result)
   125  	}
   126  
   127  	configuration = config.Config{Verbose: true}
   128  	result = ValidateFile("./../../testfiles/line-to-long.txt", configuration)
   129  	if len(result) != 1 {
   130  		t.Error("Should have no error, got", result)
   131  	}
   132  
   133  	configuration = config.Config{Verbose: true}
   134  	configuration.Disable.MaxLineLength = true
   135  	result = ValidateFile("./../../testfiles/line-to-long.txt", configuration)
   136  	if len(result) != 0 {
   137  		t.Error("Should have no error, got", result)
   138  	}
   139  }