github.com/openconfig/goyang@v1.4.5/pkg/yang/file_test.go (about)

     1  // Copyright 2015 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yang
    16  
    17  import (
    18  	"errors"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestFindFile(t *testing.T) {
    27  	sep := string(os.PathSeparator)
    28  
    29  	for _, tt := range []struct {
    30  		name  string
    31  		path  []string
    32  		check []string
    33  	}{
    34  		{
    35  			name:  "one",
    36  			check: []string{"one.yang"},
    37  		},
    38  		{
    39  			name:  "./two",
    40  			check: []string{"./two"},
    41  		},
    42  		{
    43  			name:  "three.yang",
    44  			check: []string{"three.yang"},
    45  		},
    46  		{
    47  			name:  "four",
    48  			path:  []string{"dir1", "dir2"},
    49  			check: []string{"four.yang", "dir1" + sep + "four.yang", "dir2" + sep + "four.yang"},
    50  		},
    51  	} {
    52  		var checked []string
    53  		ms := NewModules()
    54  		ms.Path = tt.path
    55  		readFile = func(path string) ([]byte, error) {
    56  			checked = append(checked, path)
    57  			return nil, errors.New("no such file")
    58  		}
    59  		scanDir = func(dir, name string, recurse bool) string {
    60  			return filepath.Join(dir, name)
    61  		}
    62  		if _, _, err := ms.findFile(tt.name); err == nil {
    63  			t.Errorf("%s unexpectedly succeeded", tt.name)
    64  			continue
    65  		}
    66  		if !reflect.DeepEqual(tt.check, checked) {
    67  			t.Errorf("%s: got %v, want %v", tt.name, checked, tt.check)
    68  		}
    69  	}
    70  }
    71  
    72  func TestScanForPathsAndAddModules(t *testing.T) {
    73  	// disable any readFile mock setup by other tests
    74  	readFile = ioutil.ReadFile
    75  
    76  	// Scan the directory tree for YANG modules
    77  	paths, err := PathsWithModules("../../testdata")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	// we should have seen two directories being testdata and
    82  	// testdata/subdir.
    83  	if len(paths) != 2 {
    84  		t.Errorf("got %d paths imported, want 2", len(paths))
    85  	}
    86  	ms := NewModules()
    87  	// add the paths found in the scan to the module path
    88  	ms.AddPath(paths...)
    89  
    90  	// confirm we can load the four modules that exist in
    91  	// the two paths we scanned.
    92  	modules := []string{"aug", "base", "other", "subdir1"}
    93  	for _, name := range modules {
    94  		if _, err := ms.GetModule(name); err != nil {
    95  			t.Errorf("getting %s: %v", name, err)
    96  		}
    97  	}
    98  
    99  	// however, a sub module is not a valid argument to GetModule.
   100  	if _, err := ms.GetModule("sub"); err == nil {
   101  		t.Error("want an error when loading 'sub', got nil")
   102  	}
   103  
   104  }
   105  
   106  func TestFindInDir(t *testing.T) {
   107  	testDir := "testdata/find-file-test"
   108  
   109  	tests := []struct {
   110  		desc      string
   111  		inDir     string
   112  		inName    string
   113  		inRecurse bool
   114  		want      string
   115  	}{{
   116  		desc:      "file not found",
   117  		inDir:     testDir,
   118  		inName:    "green.yang",
   119  		inRecurse: true,
   120  		want:      "",
   121  	}, {
   122  		desc:      "input directory does not exist",
   123  		inDir:     filepath.Join(testDir, "dne"),
   124  		inName:    "red.yang",
   125  		inRecurse: true,
   126  		want:      "",
   127  	}, {
   128  		desc:      "exact match",
   129  		inDir:     testDir,
   130  		inName:    "blue.yang",
   131  		inRecurse: false,
   132  		want:      filepath.Join(testDir, "blue.yang"),
   133  	}, {
   134  		desc:      "exact match, recursive",
   135  		inDir:     testDir,
   136  		inName:    "blue.yang",
   137  		inRecurse: true,
   138  		want:      filepath.Join(testDir, "blue.yang"),
   139  	}, {
   140  		desc:      "exact match with non-standard name",
   141  		inDir:     testDir,
   142  		inName:    "non-standard.name",
   143  		inRecurse: false,
   144  		want:      filepath.Join(testDir, "non-standard.name"),
   145  	}, {
   146  		desc:      "revision match without recursion, and ignoring invalid revision",
   147  		inDir:     testDir,
   148  		inName:    "red.yang",
   149  		inRecurse: false,
   150  		want:      filepath.Join(testDir, "red@2010-10-10.yang"),
   151  	}, {
   152  		desc:      "revision match with recursion",
   153  		inDir:     testDir,
   154  		inName:    "red.yang",
   155  		inRecurse: true,
   156  		want:      filepath.Join(testDir, "dir", "dirdir", "red@2022-02-22.yang"),
   157  	}}
   158  
   159  	for _, tt := range tests {
   160  		t.Run(tt.desc, func(t *testing.T) {
   161  			if got, want := findInDir(tt.inDir, tt.inName, tt.inRecurse), tt.want; got != want {
   162  				t.Errorf("got: %q, want: %q", got, want)
   163  			}
   164  		})
   165  	}
   166  }