github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/test/test_samples.go (about)

     1  //
     2  //  Copyright 2023 The AVFS authors
     3  //
     4  //  Licensed under the Apache License, Version 2.0 (the "License");
     5  //  you may not use this file except in compliance with the License.
     6  //  You may obtain a copy of the License at
     7  //
     8  //  	http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  //  Unless required by applicable law or agreed to in writing, software
    11  //  distributed under the License is distributed on an "AS IS" BASIS,
    12  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  //  See the License for the specific language governing permissions and
    14  //  limitations under the License.
    15  //
    16  
    17  package test
    18  
    19  import (
    20  	"io/fs"
    21  	"testing"
    22  
    23  	"github.com/avfs/avfs"
    24  )
    25  
    26  // dirInfo contains the sample directories.
    27  type dirInfo struct {
    28  	Path      string
    29  	Mode      fs.FileMode
    30  	WantModes []fs.FileMode
    31  }
    32  
    33  // sampleDirs returns the sample directories used by Mkdir function.
    34  func (ts *Suite) sampleDirs(testDir string) []*dirInfo {
    35  	dirs := []*dirInfo{
    36  		{Path: "/A", Mode: 0o777, WantModes: []fs.FileMode{0o777}},
    37  		{Path: "/B", Mode: 0o755, WantModes: []fs.FileMode{0o755}},
    38  		{Path: "/B/1", Mode: 0o755, WantModes: []fs.FileMode{0o755, 0o755}},
    39  		{Path: "/B/1/D", Mode: 0o700, WantModes: []fs.FileMode{0o755, 0o755, 0o700}},
    40  		{Path: "/B/1/E", Mode: 0o755, WantModes: []fs.FileMode{0o755, 0o755, 0o755}},
    41  		{Path: "/B/2", Mode: 0o750, WantModes: []fs.FileMode{0o755, 0o750}},
    42  		{Path: "/B/2/F", Mode: 0o755, WantModes: []fs.FileMode{0o755, 0o750, 0o755}},
    43  		{Path: "/B/2/F/3", Mode: 0o755, WantModes: []fs.FileMode{0o755, 0o750, 0o755, 0o755}},
    44  		{Path: "/B/2/F/3/G", Mode: 0o777, WantModes: []fs.FileMode{0o755, 0o750, 0o755, 0o755, 0o777}},
    45  		{Path: "/B/2/F/3/G/4", Mode: 0o777, WantModes: []fs.FileMode{0o755, 0o750, 0o755, 0o755, 0o777, 0o777}},
    46  		{Path: "/C", Mode: 0o750, WantModes: []fs.FileMode{0o750}},
    47  		{Path: "/C/5", Mode: 0o750, WantModes: []fs.FileMode{0o750, 0o750}},
    48  	}
    49  
    50  	for i, dir := range dirs {
    51  		dirs[i].Path = ts.vfsTest.Join(testDir, dir.Path)
    52  	}
    53  
    54  	return dirs
    55  }
    56  
    57  // sampleDirsAll returns the sample directories used by MkdirAll function.
    58  func (ts *Suite) sampleDirsAll(testDir string) []*dirInfo {
    59  	dirs := []*dirInfo{
    60  		{Path: "/H/6", Mode: 0o750, WantModes: []fs.FileMode{0o750, 0o750}},
    61  		{Path: "/H/6/I/7", Mode: 0o755, WantModes: []fs.FileMode{0o750, 0o750, 0o755, 0o755}},
    62  		{Path: "/H/6/I/7/J/8", Mode: 0o777, WantModes: []fs.FileMode{0o750, 0o750, 0o755, 0o755, 0o777, 0o777}},
    63  	}
    64  
    65  	for i, dir := range dirs {
    66  		dirs[i].Path = ts.vfsTest.Join(testDir, dir.Path)
    67  	}
    68  
    69  	return dirs
    70  }
    71  
    72  // createSampleDirs creates and returns sample directories and testDir directory if necessary.
    73  func (ts *Suite) createSampleDirs(tb testing.TB, testDir string) []*dirInfo {
    74  	vfs := ts.vfsSetup
    75  
    76  	err := vfs.MkdirAll(testDir, avfs.DefaultDirPerm)
    77  	RequireNoError(tb, err, "MkdirAll %s", testDir)
    78  
    79  	dirs := ts.sampleDirs(testDir)
    80  	for _, dir := range dirs {
    81  		err = vfs.Mkdir(dir.Path, dir.Mode)
    82  		RequireNoError(tb, err, "Mkdir %s", dir.Path)
    83  	}
    84  
    85  	return dirs
    86  }
    87  
    88  // fileInfo contains the sample files.
    89  type fileInfo struct {
    90  	Path    string
    91  	Mode    fs.FileMode
    92  	Content []byte
    93  }
    94  
    95  // sampleFiles returns the sample files.
    96  func (ts *Suite) sampleFiles(testDir string) []*fileInfo {
    97  	files := []*fileInfo{
    98  		{Path: "/file.txt", Mode: avfs.DefaultFilePerm, Content: []byte("file")},
    99  		{Path: "/A/afile1.txt", Mode: 0o777, Content: []byte("afile1")},
   100  		{Path: "/A/afile2.txt", Mode: avfs.DefaultFilePerm, Content: []byte("afile2")},
   101  		{Path: "/A/afile3.txt", Mode: 0o600, Content: []byte("afile3")},
   102  		{Path: "/B/1/1file.txt", Mode: avfs.DefaultFilePerm, Content: []byte("1file")},
   103  		{Path: "/B/1/E/efile.txt", Mode: avfs.DefaultFilePerm, Content: []byte("efile")},
   104  		{Path: "/B/2/F/3/3file1.txt", Mode: 0o640, Content: []byte("3file1")},
   105  		{Path: "/B/2/F/3/3file2.txt", Mode: avfs.DefaultFilePerm, Content: []byte("3file2")},
   106  		{Path: "/B/2/F/3/G/4/4file.txt", Mode: avfs.DefaultFilePerm, Content: []byte("4file")},
   107  		{Path: "/C/cfile.txt", Mode: avfs.DefaultFilePerm, Content: []byte("cfile")},
   108  	}
   109  
   110  	for i, file := range files {
   111  		files[i].Path = ts.vfsTest.Join(testDir, file.Path)
   112  	}
   113  
   114  	return files
   115  }
   116  
   117  // createSampleFiles creates and returns the sample files.
   118  func (ts *Suite) createSampleFiles(tb testing.TB, testDir string) []*fileInfo {
   119  	vfs := ts.vfsSetup
   120  
   121  	files := ts.sampleFiles(testDir)
   122  	for _, file := range files {
   123  		err := vfs.WriteFile(file.Path, file.Content, file.Mode)
   124  		RequireNoError(tb, err, "WriteFile %s", file.Path)
   125  	}
   126  
   127  	return files
   128  }
   129  
   130  // symlinkInfo contains sample symbolic links.
   131  type symlinkInfo struct {
   132  	NewPath string
   133  	OldPath string
   134  }
   135  
   136  // sampleSymlinks returns the sample symbolic links.
   137  func (ts *Suite) sampleSymlinks(testDir string) []*symlinkInfo {
   138  	vfs := ts.vfsTest
   139  	if !vfs.HasFeature(avfs.FeatSymlink) {
   140  		return nil
   141  	}
   142  
   143  	sls := []*symlinkInfo{
   144  		{NewPath: "/A/lroot", OldPath: "/"},
   145  		{NewPath: "/lC", OldPath: "/C"},
   146  		{NewPath: "/B/1/lafile2.txt", OldPath: "/A/afile2.txt"},
   147  		{NewPath: "/B/2/lf", OldPath: "/B/2/F"},
   148  		{NewPath: "/B/2/F/3/llf", OldPath: "/B/2/lf"},
   149  		{NewPath: "/C/lllf", OldPath: "/B/2/F/3/llf"},
   150  		{NewPath: "/A/l3file2.txt", OldPath: "/C/lllf/3/3file2.txt"},
   151  		{NewPath: "/C/lNonExist", OldPath: "/A/path/to/a/non/existing/file"},
   152  	}
   153  
   154  	for i, sl := range sls {
   155  		sls[i].NewPath = vfs.Join(testDir, sl.NewPath)
   156  		sls[i].OldPath = vfs.Join(testDir, sl.OldPath)
   157  	}
   158  
   159  	return sls
   160  }
   161  
   162  // symlinkEvalInfo contains the data to evaluate the sample symbolic links.
   163  type symlinkEvalInfo struct {
   164  	NewPath   string      // Name of the symbolic link.
   165  	OldPath   string      // Value of the symbolic link.
   166  	IsSymlink bool        //
   167  	Mode      fs.FileMode //
   168  }
   169  
   170  // sampleSymlinksEval returns the sample symbolic links to evaluate.
   171  func (ts *Suite) sampleSymlinksEval(testDir string) []*symlinkEvalInfo {
   172  	vfs := ts.vfsTest
   173  	if !vfs.HasFeature(avfs.FeatSymlink) {
   174  		return nil
   175  	}
   176  
   177  	sles := []*symlinkEvalInfo{
   178  		{NewPath: "/A/lroot/", OldPath: "/", IsSymlink: true, Mode: fs.ModeDir | 0o777},
   179  		{NewPath: "/A/lroot/B", OldPath: "/B", IsSymlink: false, Mode: fs.ModeDir | 0o755},
   180  		{NewPath: "/", OldPath: "/", IsSymlink: false, Mode: fs.ModeDir | 0o777},
   181  		{NewPath: "/lC", OldPath: "/C", IsSymlink: true, Mode: fs.ModeDir | 0o750},
   182  		{NewPath: "/B/1/lafile2.txt", OldPath: "/A/afile2.txt", IsSymlink: true, Mode: 0o644},
   183  		{NewPath: "/B/2/lf", OldPath: "/B/2/F", IsSymlink: true, Mode: fs.ModeDir | 0o755},
   184  		{NewPath: "/B/2/F/3/llf", OldPath: "/B/2/F", IsSymlink: true, Mode: fs.ModeDir | 0o755},
   185  		{NewPath: "/C/lllf", OldPath: "/B/2/F", IsSymlink: true, Mode: fs.ModeDir | 0o755},
   186  		{NewPath: "/B/2/F/3/llf", OldPath: "/B/2/F", IsSymlink: true, Mode: fs.ModeDir | 0o755},
   187  		{NewPath: "/C/lllf/3/3file1.txt", OldPath: "/B/2/F/3/3file1.txt", IsSymlink: false, Mode: 0o640},
   188  	}
   189  
   190  	for i, sle := range sles {
   191  		sles[i].NewPath = vfs.Join(testDir, sle.NewPath)
   192  		sles[i].OldPath = vfs.Join(testDir, sle.OldPath)
   193  	}
   194  
   195  	return sles
   196  }
   197  
   198  // createSampleSymlinks creates the sample symbolic links.
   199  func (ts *Suite) createSampleSymlinks(tb testing.TB, testDir string) []*symlinkInfo {
   200  	vfs := ts.vfsSetup
   201  
   202  	symlinks := ts.sampleSymlinks(testDir)
   203  	for _, sl := range symlinks {
   204  		err := vfs.Symlink(sl.OldPath, sl.NewPath)
   205  		RequireNoError(tb, err, "Symlink %s %s", sl.OldPath, sl.NewPath)
   206  	}
   207  
   208  	return symlinks
   209  }