github.com/samlitowitz/goimportcycle@v1.0.9/internal/directory/emitter_test.go (about)

     1  package directory_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/samlitowitz/goimportcycle/internal/directory"
    10  )
    11  
    12  func TestEmitter_WalkDirFunc_EmitsNothingAfterError(t *testing.T) {
    13  	// REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L600
    14  	// -- START -- //
    15  	if runtime.GOOS == "ios" {
    16  		restore := chtmpdir(t)
    17  		defer restore()
    18  	}
    19  
    20  	tmpDir := t.TempDir()
    21  
    22  	origDir, err := os.Getwd()
    23  	if err != nil {
    24  		t.Fatal("finding working dir:", err)
    25  	}
    26  	if err = os.Chdir(tmpDir); err != nil {
    27  		t.Fatal("entering temp dir:", err)
    28  	}
    29  	defer os.Chdir(origDir)
    30  	// -- END -- //
    31  
    32  	emitter, output := directory.NewEmitter()
    33  	go func(output <-chan string) {
    34  		for range output {
    35  			t.Fatal("no directories should be emitted")
    36  		}
    37  	}(output)
    38  
    39  	err = filepath.WalkDir(tmpDir+"/NONEXISTENT_DIR", emitter.WalkDirFunc)
    40  	if err == nil {
    41  		t.Fatal("failed to emit error")
    42  	}
    43  	emitter.Close()
    44  }
    45  
    46  func TestEmitter_WalkDirFunc_EmitsAppropriateDirectories(t *testing.T) {
    47  	// REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L600
    48  	// -- START -- //
    49  	if runtime.GOOS == "ios" {
    50  		restore := chtmpdir(t)
    51  		defer restore()
    52  	}
    53  
    54  	tmpDir := t.TempDir()
    55  
    56  	origDir, err := os.Getwd()
    57  	if err != nil {
    58  		t.Fatal("finding working dir:", err)
    59  	}
    60  	if err = os.Chdir(tmpDir); err != nil {
    61  		t.Fatal("entering temp dir:", err)
    62  	}
    63  	defer os.Chdir(origDir)
    64  	// -- END -- //
    65  
    66  	tree := &Node{
    67  		"testdata",
    68  		[]*Node{
    69  			{"a.go", nil},
    70  			{"b", []*Node{}},
    71  			{"c.go", nil},
    72  			{
    73  				"d",
    74  				[]*Node{
    75  					{"e.go", nil},
    76  					{"f", []*Node{}},
    77  					{
    78  						"g",
    79  						[]*Node{
    80  							{"h.go", nil},
    81  						},
    82  					},
    83  				},
    84  			},
    85  		},
    86  	}
    87  
    88  	expectedDirectories := makeTree(t, tree)
    89  
    90  	emitter, output := directory.NewEmitter()
    91  	go func(output <-chan string, expectedFiles map[string]struct{}) {
    92  		for actualPath := range output {
    93  			if _, ok := expectedFiles[actualPath]; !ok {
    94  				t.Fatalf("unexpected path: %s", actualPath)
    95  			}
    96  			delete(expectedFiles, actualPath)
    97  		}
    98  		if len(expectedFiles) > 0 {
    99  			missedPaths := ""
   100  			for expectedFile := range expectedFiles {
   101  				missedPaths += ", " + expectedFile
   102  			}
   103  			t.Fatalf(
   104  				"not all expected paths sent: missing %s",
   105  				missedPaths,
   106  			)
   107  		}
   108  	}(output, expectedDirectories)
   109  	err = filepath.WalkDir(tree.name, emitter.WalkDirFunc)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	emitter.Close()
   114  }
   115  
   116  // REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L449
   117  type Node struct {
   118  	name    string
   119  	entries []*Node // nil if the entry is a file
   120  }
   121  
   122  // REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L481
   123  func walkTree(n *Node, path string, f func(path string, n *Node)) {
   124  	f(path, n)
   125  	for _, e := range n.entries {
   126  		walkTree(e, filepath.Join(path, e.name), f)
   127  	}
   128  }
   129  
   130  // REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L488
   131  func makeTree(t *testing.T, tree *Node) map[string]struct{} {
   132  	directories := make(map[string]struct{})
   133  	walkTree(tree, tree.name, func(path string, n *Node) {
   134  		if n.entries == nil {
   135  			fd, err := os.Create(path)
   136  			if err != nil {
   137  				t.Errorf("makeTree: %v", err)
   138  				return
   139  			}
   140  			fd.Close()
   141  		} else {
   142  			os.Mkdir(path, 0770)
   143  			directories[path] = struct{}{}
   144  		}
   145  	})
   146  	return directories
   147  }
   148  
   149  // REFURL: https://github.com/golang/go/blob/988b718f4130ab5b3ce5a5774e1a58e83c92a163/src/path/filepath/path_test.go#L553
   150  func chtmpdir(t *testing.T) (restore func()) {
   151  	oldwd, err := os.Getwd()
   152  	if err != nil {
   153  		t.Fatalf("chtmpdir: %v", err)
   154  	}
   155  	d, err := os.MkdirTemp("", "test")
   156  	if err != nil {
   157  		t.Fatalf("chtmpdir: %v", err)
   158  	}
   159  	if err := os.Chdir(d); err != nil {
   160  		t.Fatalf("chtmpdir: %v", err)
   161  	}
   162  	return func() {
   163  		if err := os.Chdir(oldwd); err != nil {
   164  			t.Fatalf("chtmpdir: %v", err)
   165  		}
   166  		os.RemoveAll(d)
   167  	}
   168  }