github.com/KEINOS/go-countline@v1.1.1-0.20221217083629-60710df7606b/cl/_gen/gen_test_data_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/zenizh/go-capturer"
    11  )
    12  
    13  //nolint:paralleltest // do not parallelize due to temporary changing the global variable
    14  func Test_main(t *testing.T) {
    15  	// Prepare to change directory
    16  	pathReturn, err := os.Getwd()
    17  	require.NoError(t, err, "failed to get current working directory")
    18  
    19  	defer require.NoError(t, os.Chdir(pathReturn))
    20  
    21  	// Mock the path to the Docker environment file. Due to cover the case when
    22  	// the tests are running in Docker.
    23  	if IsDocker() {
    24  		oldPathDockerEnv := pathDockerEnv
    25  		defer func() {
    26  			pathDockerEnv = oldPathDockerEnv
    27  		}()
    28  
    29  		pathDockerEnv = "/foo/bar"
    30  	}
    31  
    32  	// Create test data directory under temp dir
    33  	pathDirTemp := t.TempDir()
    34  
    35  	pathDirTempData := filepath.Join(pathDirTemp, "testdata")
    36  	require.NoError(t, os.MkdirAll(pathDirTempData, 0o755), "failed to create temp directory")
    37  
    38  	// Chenge directory to the temp dir
    39  	require.NoError(t, os.Chdir(pathDirTemp))
    40  
    41  	// Mock testdata and os.Exit function
    42  	oldDataSizes := DataSizes
    43  	oldOsExit := OsExit
    44  
    45  	defer func() {
    46  		DataSizes = oldDataSizes
    47  		OsExit = oldOsExit
    48  	}()
    49  
    50  	OsExit = func(code int) {
    51  		panic("panic insted of os.Exit")
    52  	}
    53  
    54  	DataSizes = []struct {
    55  		Name string
    56  		Size int
    57  	}{
    58  		{Name: "Dummy1", Size: 32},
    59  		{Name: "Dummy2", Size: 1024 * 1024},
    60  	}
    61  
    62  	// Test
    63  	require.NotPanics(t, func() {
    64  		main()
    65  	})
    66  
    67  	require.FileExists(t, filepath.Join(pathDirTempData, "data_Dummy1.txt"), "test data not generated")
    68  	require.FileExists(t, filepath.Join(pathDirTempData, "data_Dummy2.txt"), "test data not generated")
    69  
    70  	// Re-run test and use generated files
    71  	require.NotPanics(t, func() {
    72  		main()
    73  	})
    74  }
    75  
    76  //nolint:paralleltest // do not parallelize due to temporary changing the function variable
    77  func Test_exitOnError(t *testing.T) {
    78  	// Backup and defer restore
    79  	oldOsExit := OsExit
    80  	defer func() {
    81  		OsExit = oldOsExit
    82  	}()
    83  
    84  	capturedStatus := 0
    85  
    86  	// Mock the os.Exit function
    87  	OsExit = func(code int) {
    88  		capturedStatus = code
    89  	}
    90  
    91  	out := capturer.CaptureStderr(func() {
    92  		exitOnError(errors.New("forced error"))
    93  	})
    94  
    95  	require.Equal(t, 1, capturedStatus, "it should exit with status 1")
    96  	require.Contains(t, out, "forced error", "it should print the error message to STDERR")
    97  }
    98  
    99  //nolint:paralleltest // do not parallelize due to temporary changing global variables
   100  func Test_genFiles_fail_generate_file(t *testing.T) {
   101  	// Mock the bufio.Writer to fail
   102  	forceFailWraite = true
   103  	defer func() {
   104  		forceFailWraite = false
   105  	}()
   106  
   107  	// Mock testdata and os.Exit function
   108  	oldDataSizes := DataSizes
   109  	defer func() {
   110  		DataSizes = oldDataSizes
   111  	}()
   112  
   113  	DataSizes = []struct {
   114  		Name string
   115  		Size int
   116  	}{
   117  		{Name: "Dummy1", Size: 32},
   118  	}
   119  
   120  	err := genFiles(t.TempDir())
   121  
   122  	require.Error(t, err)
   123  	require.Contains(t, err.Error(), "failed to write line",
   124  		"it should contain the error reason if failed to writer")
   125  }
   126  
   127  func Test_genFile(t *testing.T) {
   128  	t.Parallel()
   129  
   130  	pathFileTemp := filepath.Join(t.TempDir(), "test_"+t.Name()+".txt")
   131  
   132  	// Generate a file with 16 bytes in size
   133  	err := genFile(16, pathFileTemp)
   134  
   135  	require.NoError(t, err, "failed to generate file")
   136  	require.FileExists(t, pathFileTemp, "file not generated")
   137  
   138  	// Test content
   139  	expect := []byte("line: 1\nline: 2\n")
   140  	actual, err := os.ReadFile(pathFileTemp)
   141  
   142  	require.NoError(t, err, "failed to read generated file")
   143  	require.Equal(t, string(expect), string(actual), "generated file content mismatch")
   144  }
   145  
   146  func Test_genFile_file_is_dir(t *testing.T) {
   147  	t.Parallel()
   148  
   149  	err := genFile(16, t.TempDir())
   150  
   151  	require.Error(t, err, "it should fail if the path is a directory")
   152  	require.Contains(t, err.Error(), "failed to open/create file", "it should contain the error reason")
   153  }
   154  
   155  //nolint:paralleltest // do not parallelize due to temporary changing the global variable
   156  func TestIsDocker(t *testing.T) {
   157  	oldPathDockerEnv := pathDockerEnv
   158  	defer func() {
   159  		pathDockerEnv = oldPathDockerEnv
   160  	}()
   161  
   162  	pathDummy := filepath.Join(t.TempDir(), "docker_env_dummy")
   163  	require.NoError(t, os.WriteFile(pathDummy, []byte{}, os.ModeTemporary))
   164  
   165  	// Mock the path to the Docker environment file
   166  	pathDockerEnv = pathDummy
   167  
   168  	// Test in Docker
   169  	require.True(t, IsDocker(), "it should return true if running in Docker")
   170  }