18
"bytes"
19
"os"
20
"path/filepath"
21
+ "runtime"
22
"strings"
23
"testing"
24
)
480
}
481
}
482
483
+// TestParseImportInaccessibleFile ensures that an import path which exists
484
+// but cannot be accessed reports the underlying OS error instead of the
485
+// misleading "File to import not found" (issue #8161). A path through a
486
+// regular file fails stat with ENOTDIR, unlike a genuinely missing file,
487
+// which must keep the "not found" message. Windows reports that same path
488
+// as not existing instead, so the through-a-file case is skipped there;
489
+// the permission-denied case from the issue is unaffected.
490
+func TestParseImportInaccessibleFile(t *testing.T) {
491
+ testParseOne := func(input string) (ServerBlock, error) {
492
+ p := testParser(input)
493
+ p.Next() // parseOne doesn't call Next() to start, so we must
494
+ err := p.parseOne()
495
+ return p.block, err
496
+ }
497
+
498
+ if runtime.GOOS != "windows" {
499
+ _, err := testParseOne("import testdata/import_test1.txt/child.txt")
500
+ if err == nil {
501
+ t.Fatal("expected error importing through a regular file, got nil")
502
+ }
503
+ if strings.Contains(err.Error(), "not found") {
504
+ t.Errorf("expected OS error to be reported, got: %v", err)
505
+ }
506
+ if !strings.Contains(err.Error(), "child.txt") {
507
+ t.Errorf("expected error to name the import path, got: %v", err)
508
+ }
509
+ }
510
+
511
+ _, err := testParseOne("import testdata/not_found.txt")
512
+ if err == nil {
513
+ t.Fatal("expected error importing a missing file, got nil")
514
+ }
515
+ if !strings.Contains(err.Error(), "File to import not found") {
516
+ t.Errorf("expected 'File to import not found' for a missing file, got: %v", err)
517
+ }
518
+}
519
+
520
func TestDirectiveImport(t *testing.T) {
521
testParseOne := func(input string) (ServerBlock, error) {
522
p := testParser(input)