zombiezen.com/go/lua@v0.0.0-20231013005828-290725fb9140/iolib_test.go (about)

     1  // Copyright 2023 Ross Light
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  // this software and associated documentation files (the “Software”), to deal in
     5  // the Software without restriction, including without limitation the rights to
     6  // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
     7  // the Software, and to permit persons to whom the Software is furnished to do so,
     8  // subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in all
    11  // 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, FITNESS
    15  // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    16  // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    17  // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    18  // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    19  //
    20  // SPDX-License-Identifier: MIT
    21  
    22  package lua
    23  
    24  import (
    25  	"io"
    26  	"os"
    27  	"path/filepath"
    28  	"runtime"
    29  	"strings"
    30  	"testing"
    31  )
    32  
    33  func TestIOLibrary(t *testing.T) {
    34  	t.Run("Files", func(t *testing.T) {
    35  		lib := NewIOLibrary()
    36  		lib.Stdin = nil
    37  		lib.Stdout = nil
    38  		lib.Stderr = nil
    39  		lib.OpenProcessReader = nil
    40  		lib.OpenProcessWriter = nil
    41  
    42  		dir := t.TempDir()
    43  		origOpen := lib.Open
    44  		lib.Open = func(name, mode string) (io.Closer, error) {
    45  			cleaned := filepath.Clean(name)
    46  			if cleaned == "." || cleaned == ".." || strings.ContainsRune(cleaned, filepath.Separator) {
    47  				return nil, &os.PathError{
    48  					Op:   "open",
    49  					Path: name,
    50  					Err:  os.ErrNotExist,
    51  				}
    52  			}
    53  			f, err := origOpen(filepath.Join(dir, cleaned), mode)
    54  			if e, ok := err.(*os.PathError); ok {
    55  				e.Path = name
    56  			}
    57  			return f, err
    58  		}
    59  
    60  		state := new(State)
    61  		defer func() {
    62  			if err := state.Close(); err != nil {
    63  				t.Error("Close:", err)
    64  			}
    65  		}()
    66  		out := new(strings.Builder)
    67  		openBase := NewOpenBase(out, nil)
    68  		if err := Require(state, GName, true, openBase); err != nil {
    69  			t.Error(err)
    70  		}
    71  		if err := Require(state, IOLibraryName, true, lib.OpenLibrary); err != nil {
    72  			t.Error(err)
    73  		}
    74  
    75  		f, err := os.Open(filepath.Join("testdata", "iolib.lua"))
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  		defer f.Close()
    80  		if err := state.Load(f, "@testdata/iolib.lua", "t"); err != nil {
    81  			t.Fatal(err)
    82  		}
    83  		err = state.Call(0, 0, 0)
    84  		if out.Len() > 0 {
    85  			t.Log(out.String())
    86  		}
    87  		if err != nil {
    88  			t.Error(err)
    89  		}
    90  	})
    91  
    92  	t.Run("Popen", func(t *testing.T) {
    93  		if runtime.GOOS == "windows" {
    94  			t.Skip("Not running popen test on Windows")
    95  		}
    96  		wd, err := os.Getwd()
    97  		if err != nil {
    98  			t.Fatal(err)
    99  		}
   100  		t.Cleanup(func() {
   101  			os.Chdir(wd)
   102  		})
   103  
   104  		lib := NewIOLibrary()
   105  		lib.Stdin = nil
   106  		lib.Stdout = nil
   107  		lib.Stderr = nil
   108  
   109  		state := new(State)
   110  		defer func() {
   111  			if err := state.Close(); err != nil {
   112  				t.Error("Close:", err)
   113  			}
   114  		}()
   115  		out := new(strings.Builder)
   116  		openBase := NewOpenBase(out, nil)
   117  		if err := Require(state, GName, true, openBase); err != nil {
   118  			t.Error(err)
   119  		}
   120  		if err := Require(state, IOLibraryName, true, lib.OpenLibrary); err != nil {
   121  			t.Error(err)
   122  		}
   123  
   124  		f, err := os.Open(filepath.Join("testdata", "popen_unix.lua"))
   125  		if err != nil {
   126  			t.Fatal(err)
   127  		}
   128  		defer f.Close()
   129  		if err := state.Load(f, "@testdata/popen_unix.lua", "t"); err != nil {
   130  			t.Fatal(err)
   131  		}
   132  		if err := os.Chdir(t.TempDir()); err != nil {
   133  			t.Fatal(err)
   134  		}
   135  		err = state.Call(0, 0, 0)
   136  		if out.Len() > 0 {
   137  			t.Log(out.String())
   138  		}
   139  		if err != nil {
   140  			t.Error(err)
   141  		}
   142  	})
   143  }