go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/os/stream_file_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  	"io"
    25  	"io/fs"
    26  	goos "os"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  	"go.mway.dev/x/os"
    32  	"go.mway.dev/x/os/tempdir"
    33  )
    34  
    35  func TestWithFileReader(t *testing.T) {
    36  	err := tempdir.With(func(_ string) {
    37  		wantContent := []byte("hello")
    38  		require.NoError(t, goos.WriteFile(t.Name(), wantContent, 0o744))
    39  
    40  		var haveContent []byte
    41  		require.NoError(
    42  			t,
    43  			os.WithFileReader(
    44  				t.Name(),
    45  				func(r io.Reader) (err error) {
    46  					haveContent, err = io.ReadAll(r)
    47  					return
    48  				},
    49  			),
    50  		)
    51  		require.Equal(t, wantContent, haveContent)
    52  	})
    53  	require.NoError(t, err)
    54  }
    55  
    56  func TestWithFileReader_NotExist(t *testing.T) {
    57  	err := tempdir.With(func(_ string) {
    58  		var haveContent []byte
    59  		require.ErrorIs(
    60  			t,
    61  			os.WithFileReader(
    62  				t.Name(),
    63  				func(r io.Reader) (err error) {
    64  					haveContent, err = io.ReadAll(r)
    65  					return
    66  				},
    67  			),
    68  			goos.ErrNotExist,
    69  		)
    70  		require.Nil(t, haveContent)
    71  	})
    72  	require.NoError(t, err)
    73  }
    74  
    75  func TestWithFileWriter(t *testing.T) {
    76  	err := tempdir.With(func(_ string) {
    77  		wantContent := []byte("hello")
    78  
    79  		require.NoError(
    80  			t,
    81  			os.WithFileWriter(
    82  				t.Name(),
    83  				func(w io.Writer) error {
    84  					_, err := w.Write(wantContent)
    85  					return err
    86  				},
    87  			),
    88  		)
    89  
    90  		haveContent, err := goos.ReadFile(t.Name())
    91  		require.NoError(t, err)
    92  		require.Equal(t, wantContent, haveContent)
    93  	})
    94  	require.NoError(t, err)
    95  }
    96  
    97  func TestWithFileWriter_NotExist(t *testing.T) {
    98  	err := tempdir.With(func(_ string) {
    99  		require.ErrorIs(
   100  			t,
   101  			os.WithFileWriter(
   102  				filepath.Join("dne", t.Name()),
   103  				func(w io.Writer) error {
   104  					_, err := w.Write([]byte("won't be written"))
   105  					return err
   106  				},
   107  			),
   108  			goos.ErrNotExist,
   109  		)
   110  
   111  		_, err := goos.ReadFile(t.Name())
   112  		require.ErrorIs(t, err, goos.ErrNotExist)
   113  	})
   114  	require.NoError(t, err)
   115  }
   116  
   117  func TestWithFileModeWriter(t *testing.T) {
   118  	err := tempdir.With(func(_ string) {
   119  		var (
   120  			wantContent = []byte("hello")
   121  			wantMode    = fs.FileMode(0o741) // intentionally unusual permission
   122  		)
   123  
   124  		require.NoError(
   125  			t,
   126  			os.WithFileModeWriter(
   127  				t.Name(),
   128  				wantMode,
   129  				func(w io.Writer) error {
   130  					_, err := w.Write(wantContent)
   131  					return err
   132  				},
   133  			),
   134  		)
   135  
   136  		haveContent, err := goos.ReadFile(t.Name())
   137  		require.NoError(t, err)
   138  		require.Equal(t, wantContent, haveContent)
   139  
   140  		haveStat, err := goos.Stat(t.Name())
   141  		require.NoError(t, err)
   142  		require.Equal(t, wantMode.String(), (haveStat.Mode() & fs.ModePerm).String())
   143  	})
   144  	require.NoError(t, err)
   145  }