go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/os/mkdir_inherit_test.go (about)

     1  // Copyright (c) 2024 Matt Way
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to
     5  // deal in the Software without restriction, including without limitation the
     6  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     7  // sell copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    19  // IN THE THE SOFTWARE.
    20  
    21  package os_test
    22  
    23  import (
    24  	goos "os"
    25  	gofilepath "path/filepath"
    26  	"sort"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/require"
    30  	"go.mway.dev/x/os"
    31  	"golang.org/x/exp/maps"
    32  )
    33  
    34  var (
    35  	_modeTree = map[string]goos.FileMode{
    36  		"foo":                 0o755,
    37  		"foo/bar":             0o765,
    38  		"foo/bar/baz":         0o766,
    39  		"foo/bar/baz/bat":     0o777,
    40  		"foo/bar/baz/bat/qux": 0o777,
    41  	}
    42  	_modePaths = func() []string {
    43  		paths := maps.Keys(_modeTree)
    44  		sort.Slice(paths, func(i int, j int) bool {
    45  			return len(paths[i]) < len(paths[j])
    46  		})
    47  		return paths
    48  	}()
    49  )
    50  
    51  func TestMkdirAllInherit(t *testing.T) {
    52  	tempdir := createModeTree(t)
    53  
    54  	for _, path := range _modePaths {
    55  		t.Run(path, func(t *testing.T) {
    56  			path = gofilepath.Join(tempdir, path)
    57  
    58  			newdir := gofilepath.Join(path, "newdir")
    59  			require.NoError(t, os.MkdirAllInherit(newdir))
    60  			requireModePermsMatch(t, path, newdir)
    61  		})
    62  	}
    63  }
    64  
    65  func TestMkdirAllInheritRelative(t *testing.T) {
    66  	tempdir := createModeTree(t)
    67  
    68  	for _, path := range _modePaths {
    69  		t.Run(path, func(t *testing.T) {
    70  			err := os.WithCwd(tempdir, func() {
    71  				newdir := gofilepath.Join(path, "newdir")
    72  				require.NoError(t, os.MkdirAllInherit(newdir))
    73  				requireModePermsMatch(t, path, newdir)
    74  			})
    75  			require.NoError(t, err)
    76  		})
    77  	}
    78  }
    79  
    80  func createModeTree(t *testing.T) string {
    81  	tempdir := t.TempDir()
    82  
    83  	for _, path := range _modePaths {
    84  		fqpath := gofilepath.Join(tempdir, path)
    85  		require.NoError(t, goos.MkdirAll(fqpath, _modeTree[path]))
    86  	}
    87  
    88  	return tempdir
    89  }
    90  
    91  func requireModePermsMatch(t *testing.T, a string, b string) {
    92  	t.Helper()
    93  
    94  	astat, aerr := goos.Stat(a)
    95  	require.NoErrorf(t, aerr, "failed to stat %q", a)
    96  
    97  	bstat, berr := goos.Stat(b)
    98  	require.NoErrorf(t, berr, "failed to stat %q", b)
    99  
   100  	require.Equal(t, astat.Mode()&goos.ModePerm, bstat.Mode()&goos.ModePerm)
   101  }