github.com/per1234/editorconfig-checker/v2@v2.8.5/pkg/files/files_test.go (about)

     1  package files
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"regexp"
     7  	"runtime"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/per1234/editorconfig-checker/v2/pkg/config"
    12  )
    13  
    14  func TestGetContentType(t *testing.T) {
    15  	configuration := config.Config{}
    16  	inputFile := "./files.go"
    17  	expected := "text/plain"
    18  	contentType, _ := GetContentType(inputFile, configuration)
    19  	if !strings.Contains(contentType, expected) {
    20  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, expected, contentType)
    21  	}
    22  
    23  	inputFile = "./../../docs/logo.png"
    24  	expected = "image/png"
    25  	contentType, _ = GetContentType(inputFile, configuration)
    26  	if !strings.Contains(contentType, expected) {
    27  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, expected, contentType)
    28  	}
    29  
    30  	inputFile = "."
    31  	_, err := GetContentType(inputFile, configuration)
    32  	if err == nil {
    33  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, "an error", "nil")
    34  	}
    35  
    36  	inputFile = "a non-existent file"
    37  	_, err = GetContentType(inputFile, configuration)
    38  	if err == nil {
    39  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, "an error", "nil")
    40  	}
    41  
    42  	inputFile = "testdata/empty.txt"
    43  	contentType, err = GetContentType(inputFile, configuration)
    44  	if err != nil {
    45  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, "nil", err.Error())
    46  	}
    47  	expected = ""
    48  	if contentType != expected {
    49  		t.Errorf("GetContentType(%q): expected %v, got %v", inputFile, expected, contentType)
    50  	}
    51  }
    52  
    53  func TestIsAllowedContentType(t *testing.T) {
    54  	configuration := config.Config{AllowedContentTypes: []string{"text/", "application/octet-stream"}}
    55  	isAllowedContentTypeTests := []struct {
    56  		contentType string
    57  		config      config.Config
    58  		expected    bool
    59  	}{
    60  		{"bla", configuration, false},
    61  		{"text/", configuration, true},
    62  		{"text/xml abc", configuration, true},
    63  	}
    64  
    65  	for _, tt := range isAllowedContentTypeTests {
    66  		actual := IsAllowedContentType(tt.contentType, tt.config)
    67  		if actual != tt.expected {
    68  			t.Errorf("IsAllowedContentType(%s, %+v): expected: %v, got: %v", tt.contentType, tt.config, tt.expected, actual)
    69  		}
    70  	}
    71  }
    72  
    73  func TestPathExists(t *testing.T) {
    74  	pathExistsTests := []struct {
    75  		path     string
    76  		expected bool
    77  	}{
    78  		{".", true},
    79  		{"notexisting", false},
    80  	}
    81  
    82  	for _, tt := range pathExistsTests {
    83  		actual := PathExists(tt.path)
    84  		if actual != tt.expected {
    85  			t.Errorf("PathExists(%s): expected: %v, got: %v", tt.path, tt.expected, actual)
    86  		}
    87  	}
    88  }
    89  
    90  func TestGetRelativePath(t *testing.T) {
    91  	// Should return paths that are already relative unchanged
    92  	relativeFilePath, _ := GetRelativePath("bin/ec")
    93  	if relativeFilePath != "bin/ec" {
    94  		t.Errorf("GetRelativePath(%s): expected: %v, got: %v", "bin/ec", "bin/ec", relativeFilePath)
    95  	}
    96  
    97  	// Should convert absolute paths to be relative to current directory
    98  	cwd, _ := os.Getwd()
    99  	filePath := "/bin/ec"
   100  	relativeFilePath, _ = GetRelativePath(cwd + filePath)
   101  
   102  	if relativeFilePath != "bin/ec" {
   103  		t.Errorf("GetRelativePath(%s): expected: %v, got: %v", cwd+filePath, "bin/ec", relativeFilePath)
   104  	}
   105  
   106  	if runtime.GOOS == "windows" {
   107  		t.Skip("Windows fails if current directory is deleted")
   108  	}
   109  
   110  	DIR := "/tmp/stuff"
   111  	os.Remove(DIR)
   112  	err := os.Mkdir(DIR, 0755)
   113  	if err != nil {
   114  		panic(err)
   115  	}
   116  
   117  	err = os.Chdir(DIR)
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  
   122  	// Check with the current directory ("/tmp/stuff") in the middle of the given file path
   123  	relativeFilePath, _ = GetRelativePath("/foo" + DIR + filePath)
   124  	if relativeFilePath != "../../foo"+DIR+filePath {
   125  		t.Errorf("GetRelativePath(%s): expected: %v, got: %v", "/foo"+DIR+filePath, "../../foo"+DIR+filePath, relativeFilePath)
   126  	}
   127  
   128  	err = os.Remove(DIR)
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	_, err = GetRelativePath(cwd + filePath)
   134  
   135  	if err == nil {
   136  		t.Error("Expected an error for a not existing directory")
   137  	}
   138  
   139  	err = os.Chdir(cwd)
   140  	if err != nil {
   141  		panic(err)
   142  	}
   143  }
   144  
   145  func TestAddToFiles(t *testing.T) {
   146  	configuration := config.Config{}
   147  	excludedFileConfiguration := config.Config{Exclude: []string{"files"}}
   148  	addToFilesTests := []struct {
   149  		filePaths []string
   150  		filePath  string
   151  		config    config.Config
   152  		expected  []string
   153  	}{
   154  		{[]string{},
   155  			"./files.go",
   156  			excludedFileConfiguration,
   157  			[]string{}},
   158  		{[]string{"./files.go"},
   159  			"./files.go",
   160  			configuration,
   161  			[]string{"./files.go"}},
   162  	}
   163  
   164  	for _, tt := range addToFilesTests {
   165  		actual := AddToFiles(tt.filePaths, tt.filePath, tt.config)
   166  
   167  		if !reflect.DeepEqual(actual, tt.expected) {
   168  			t.Error(actual)
   169  			t.Error(tt.expected)
   170  			t.Errorf("AddToFiles(%s, %s, %+v): expected: %v, got: %v", tt.filePaths, tt.filePath, tt.config, tt.expected, actual)
   171  		}
   172  	}
   173  }
   174  
   175  func TestGetFiles(t *testing.T) {
   176  	configuration := config.Config{}
   177  	_, err := GetFiles(configuration)
   178  
   179  	if err != nil {
   180  		t.Errorf("GetFiles(): expected nil, got %s", err.Error())
   181  	}
   182  
   183  	configuration.PassedFiles = []string{"."}
   184  	files, err := GetFiles(configuration)
   185  
   186  	if len(files) > 0 && err != nil {
   187  		t.Errorf("GetFiles(.): expected nil, got %s", err.Error())
   188  	}
   189  }
   190  
   191  type getContentTypeFilesTest struct {
   192  	filename string
   193  	regex    string
   194  }
   195  
   196  var getContentTypeFilesTests = []getContentTypeFilesTest{
   197  	{"8859_1_da.html", "^text/"},
   198  	{"8859_1_de.html", "^text/"},
   199  	{"8859_1_en.html", "^text/"},
   200  	{"8859_1_es.html", "^text/"},
   201  	{"8859_1_fr.html", "^text/"},
   202  	{"8859_1_pt.html", "^text/"},
   203  	{"ascii.txt", "^text/"},
   204  	{"big5.html", "^text/"},
   205  	{"candide-gb18030.txt", "^text/"},
   206  	{"candide-utf-16le.txt", "^application/octet-stream$"}, // no BOM
   207  	{"candide-utf-32be.txt", "^application/octet-stream$"}, // no BOM
   208  	{"candide-utf-8.txt", "^text/"},                        // no BOM
   209  	{"candide-windows-1252.txt", "^text/"},
   210  	{"cp865.txt", "^text/"},
   211  	{"euc_jp.html", "^text/"},
   212  	{"euc_kr.html", "^text/"},
   213  	{"gb18030.html", "^text/"},
   214  	{"html.html", "^text/"},
   215  	{"html.iso88591.html", "^text/"},
   216  	{"html.svg.html", "^text/"},
   217  	{"html.usascii.html", "^text/"},
   218  	{"html.utf8bomdetect.html", "^text/"}, // has BOM
   219  	{"html.utf8bom.html", "^text/"},       // has BOM
   220  	{"html.utf8bomws.html", "^text/"},     // has BOM
   221  	{"html.utf8.html", "^text/"},          // no BOM
   222  	{"html.withbr.html", "^text/"},
   223  	{"iso88591.txt", "^text/"},
   224  	{"koi8_r.txt", "^text/"},
   225  	{"latin1.txt", "^text/"},
   226  	{"rashomon-euc-jp.txt", "^text/"},
   227  	{"rashomon-iso-2022-jp.txt", "^text/"}, // byte 89 is an Esc (ASCII 27)
   228  	{"rashomon-shift-jis.txt", "^text/"},
   229  	{"rashomon-utf-8.txt", "^text/"}, // no BOM
   230  	{"shift_jis.html", "^text/"},
   231  	{"sunzi-bingfa-gb-levels-1-and-2-hz-gb2312.txt", "^text/"},
   232  	{"sunzi-bingfa-gb-levels-1-and-2-utf-8.txt", "^text/"}, // no BOM
   233  	{"sunzi-bingfa-simplified-gbk.txt", "^text/"},
   234  	{"sunzi-bingfa-simplified-utf-8.txt", "^text/"}, // no BOM
   235  	{"sunzi-bingfa-traditional-big5.txt", "^text/"},
   236  	{"sunzi-bingfa-traditional-utf-8.txt", "^text/"}, // no BOM
   237  	{"unsu-joh-eun-nal-euc-kr.txt", "^text/"},
   238  	{"unsu-joh-eun-nal-utf-8.txt", "^text/"},       // no BOM
   239  	{"utf16bebom.txt", "^text/"},                   // has BOM
   240  	{"utf16lebom.txt", "^text/"},                   // has BOM
   241  	{"utf16.txt", "^text/"},                        // has BOM
   242  	{"utf32bebom.txt", "^text/"},                   // has BOM
   243  	{"utf32lebom.txt", "^text/"},                   // has BOM
   244  	{"utf8_bom.html", "^text/"},                    // has BOM
   245  	{"utf8.html", "^text/"},                        // no BOM
   246  	{"utf8-sdl.txt", "^text/"},                     // no BOM
   247  	{"utf8.txt", "^text/"},                         // no BOM
   248  	{"utf8.txt-encoding-test-files.txt", "^text/"}, // no BOM
   249  }
   250  
   251  func TestGetContentTypeFiles(t *testing.T) {
   252  	configuration := config.Config{}
   253  	for _, tt := range getContentTypeFilesTests {
   254  		filePath := "../encoding/testdata/" + tt.filename
   255  		contentType, err := GetContentType(filePath, configuration)
   256  		if err != nil {
   257  			t.Errorf("GetContentType(%q): expected %v, got %v", tt.filename, "nil", err.Error())
   258  		}
   259  		match, _ := regexp.MatchString(tt.regex, contentType)
   260  		if !match {
   261  			t.Errorf("GetContentType(%q): expected %v, got %v", tt.filename, tt.regex, contentType)
   262  		}
   263  	}
   264  }