github.com/gernest/nezuko@v0.1.2/internal/modload/init_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package modload
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  func TestFindModuleRootIgnoreDir(t *testing.T) {
    15  	// In Plan 9, directories are automatically created in /n.
    16  	// For example, /n/z.mod always exist, but it's a directory.
    17  	// Test that we ignore directories when trying to find z.mod and other config files.
    18  
    19  	dir, err := ioutil.TempDir("", "gotest")
    20  	if err != nil {
    21  		t.Fatalf("failed to create temporary directory: %v", err)
    22  	}
    23  	defer os.RemoveAll(dir)
    24  	if err := os.Mkdir(filepath.Join(dir, "z.mod"), os.ModeDir|0755); err != nil {
    25  		t.Fatalf("Mkdir failed: %v", err)
    26  	}
    27  	for _, name := range altConfigs {
    28  		if err := os.MkdirAll(filepath.Join(dir, name), os.ModeDir|0755); err != nil {
    29  			t.Fatalf("MkdirAll failed: %v", err)
    30  		}
    31  	}
    32  	p := filepath.Join(dir, "example")
    33  	if err := os.Mkdir(p, os.ModeDir|0755); err != nil {
    34  		t.Fatalf("Mkdir failed: %v", err)
    35  	}
    36  	if root, _ := FindModuleRoot(p, "", false); root != "" {
    37  		t.Errorf("FindModuleRoot(%q, \"\", false): %q, want empty string", p, root)
    38  	}
    39  	if root, _ := FindModuleRoot(p, "", true); root != "" {
    40  		t.Errorf("FindModuleRoot(%q, \"\", true): %q, want empty string", p, root)
    41  	}
    42  }