github.com/enetx/g@v1.0.80/tests/file_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"syscall"
    10  	"testing"
    11  
    12  	"github.com/enetx/g"
    13  )
    14  
    15  func TestFile_Dir_Success(t *testing.T) {
    16  	// Create a temporary file for testing
    17  	tempFile := createTempFile(t)
    18  	defer os.Remove(tempFile)
    19  
    20  	// Create a File instance representing the temporary file
    21  	file := g.NewFile(g.String(tempFile))
    22  
    23  	// Get the directory of the file
    24  	result := file.Dir()
    25  
    26  	// Check if the operation succeeded
    27  	if result.IsErr() {
    28  		t.Errorf("TestFile_Dir_Success: Unexpected error: %s", result.Err().Error())
    29  	}
    30  
    31  	// Check if the directory of the file is correct
    32  	expectedDir := filepath.Dir(tempFile)
    33  	actualDir := result.Ok().Path().Ok().Std()
    34  	if actualDir != expectedDir {
    35  		t.Errorf("TestFile_Dir_Success: Expected directory %s, got %s", expectedDir, actualDir)
    36  	}
    37  }
    38  
    39  func TestFile_Exist_Success(t *testing.T) {
    40  	// Create a temporary file for testing
    41  	tempFile := createTempFile(t)
    42  	defer os.Remove(tempFile)
    43  
    44  	// Create a File instance representing the temporary file
    45  	file := g.NewFile(g.String(tempFile))
    46  
    47  	// Check if the file exists
    48  	exists := file.Exist()
    49  
    50  	// Check if the existence check is correct
    51  	if !exists {
    52  		t.Errorf("TestFile_Exist_Success: File should exist, but it doesn't.")
    53  	}
    54  }
    55  
    56  func TestFile_MimeType_Success(t *testing.T) {
    57  	// Create a temporary file for testing
    58  	tempFile := createTempFileWithData(t, []byte("test content"))
    59  	defer os.Remove(tempFile)
    60  
    61  	// Create a File instance representing the temporary file
    62  	file := g.NewFile(g.String(tempFile))
    63  
    64  	// Get the MIME type of the file
    65  	result := file.MimeType()
    66  
    67  	// Check if the operation succeeded
    68  	if result.IsErr() {
    69  		t.Errorf("TestFile_MimeType_Success: Unexpected error: %s", result.Err().Error())
    70  	}
    71  
    72  	// Check if the detected MIME type is correct
    73  	expectedMimeType := "text/plain; charset=utf-8" // MIME type for "test content"
    74  	actualMimeType := result.Ok().Std()
    75  	if actualMimeType != expectedMimeType {
    76  		t.Errorf("TestFile_MimeType_Success: Expected MIME type %s, got %s", expectedMimeType, actualMimeType)
    77  	}
    78  }
    79  
    80  func TestFile_Read_Success(t *testing.T) {
    81  	// Create a temporary file for testing
    82  	tempFile := createTempFileWithData(t, []byte("test content"))
    83  	defer os.Remove(tempFile)
    84  
    85  	// Create a File instance representing the temporary file
    86  	file := g.NewFile(g.String(tempFile))
    87  
    88  	// Read the contents of the file
    89  	result := file.Read()
    90  
    91  	// Check if the operation succeeded
    92  	if result.IsErr() {
    93  		t.Errorf("TestFile_Read_Success: Unexpected error: %s", result.Err().Error())
    94  	}
    95  
    96  	// Check if the contents of the file are correct
    97  	expectedContent := "test content"
    98  	actualContent := result.Ok().Std()
    99  	if actualContent != expectedContent {
   100  		t.Errorf("TestFile_Read_Success: Expected content %s, got %s", expectedContent, actualContent)
   101  	}
   102  }
   103  
   104  func TestFile_IsLink_Success(t *testing.T) {
   105  	// Create a temporary file for testing
   106  	tempFile := createTempFile(t)
   107  	defer os.Remove(tempFile)
   108  
   109  	// Create a symbolic link to the temporary file
   110  	symlink := tempFile + ".link"
   111  	err := os.Symlink(tempFile, symlink)
   112  	if err != nil {
   113  		t.Fatalf("Failed to create symbolic link: %s", err)
   114  	}
   115  	defer os.Remove(symlink)
   116  
   117  	// Create a File instance representing the symbolic link
   118  	file := g.NewFile(g.String(symlink))
   119  
   120  	// Check if the file is a symbolic link
   121  	isLink := file.IsLink()
   122  
   123  	// Check if the result is correct
   124  	if !isLink {
   125  		t.Errorf("TestFile_IsLink_Success: Expected file to be a symbolic link, but it is not.")
   126  	}
   127  }
   128  
   129  func TestFile_IsLink_Failure(t *testing.T) {
   130  	// Create a temporary file for testing
   131  	tempFile := createTempFile(t)
   132  	defer os.Remove(tempFile)
   133  
   134  	// Create a File instance representing the temporary file
   135  	file := g.NewFile(g.String(tempFile))
   136  
   137  	// Check if the file is a symbolic link
   138  	isLink := file.IsLink()
   139  
   140  	// Check if the result is correct
   141  	if isLink {
   142  		t.Errorf("TestFile_IsLink_Failure: Expected file not to be a symbolic link, but it is.")
   143  	}
   144  }
   145  
   146  // createTempFileWithData creates a temporary file with the specified data for testing and returns its path.
   147  func createTempFileWithData(t *testing.T, data []byte) string {
   148  	tempFile, err := os.CreateTemp("", "testfile")
   149  	if err != nil {
   150  		t.Fatalf("Failed to create temporary file: %s", err)
   151  	}
   152  
   153  	_, err = tempFile.Write(data)
   154  	if err != nil {
   155  		t.Fatalf("Failed to write data to temporary file: %s", err)
   156  	}
   157  
   158  	err = tempFile.Close()
   159  	if err != nil {
   160  		t.Fatalf("Failed to close temporary file: %s", err)
   161  	}
   162  
   163  	return tempFile.Name()
   164  }
   165  
   166  func TestFile_Chunks_Success(t *testing.T) {
   167  	// Create a temporary file for testing
   168  	tempFile := createTempFile(t)
   169  	defer os.Remove(tempFile)
   170  
   171  	// Write content to the temporary file
   172  	content := "abcdefghijklmnopqrstuvwxyz"
   173  	writeToFile(t, tempFile, content)
   174  
   175  	// Create a File instance representing the temporary file
   176  	file := g.NewFile(g.String(tempFile))
   177  
   178  	// Define the chunk size
   179  	chunkSize := g.Int(5)
   180  
   181  	// Read the file in chunks
   182  	result := file.Chunks(chunkSize)
   183  
   184  	// Check if the result is successful
   185  	if result.IsErr() {
   186  		t.Fatalf(
   187  			"TestFile_Chunks_Success: Expected Chunks to return a successful result, but got an error: %v",
   188  			result.Err(),
   189  		)
   190  	}
   191  
   192  	// Unwrap the Result type to get the underlying iterator
   193  	iterator := result.Ok().Collect()
   194  
   195  	// Read chunks from the iterator and verify their content
   196  	expectedChunks := g.Slice[g.String]{"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"}
   197  
   198  	if iterator.Ne(expectedChunks) {
   199  		t.Fatalf(
   200  			"TestFile_Chunks_Success: Expected chunks %v, got %v",
   201  			expectedChunks,
   202  			iterator,
   203  		)
   204  	}
   205  }
   206  
   207  func TestFile_Lines_Success(t *testing.T) {
   208  	// Create a temporary file for testing
   209  	tempFile := createTempFile(t)
   210  	defer os.Remove(tempFile)
   211  
   212  	// Write content to the temporary file
   213  	content := "line1\nline2\nline3\nline4\nline5\n"
   214  	writeToFile(t, tempFile, content)
   215  
   216  	// Create a File instance representing the temporary file
   217  	file := g.NewFile(g.String(tempFile))
   218  
   219  	// Read the file line by line
   220  	result := file.Lines()
   221  
   222  	// Check if the result is successful
   223  	if result.IsErr() {
   224  		t.Fatalf(
   225  			"TestFile_Lines_Success: Expected Lines to return a successful result, but got an error: %v",
   226  			result.Err(),
   227  		)
   228  	}
   229  
   230  	// Unwrap the Result type to get the underlying iterator
   231  	iterator := result.Ok().Collect()
   232  
   233  	// Read lines from the iterator and verify their content
   234  	expectedLines := g.Slice[g.String]{"line1", "line2", "line3", "line4", "line5"}
   235  
   236  	if iterator.Ne(expectedLines) {
   237  		t.Fatalf(
   238  			"TestFile_Lines_Success: Expected lines %v, got %v",
   239  			expectedLines,
   240  			iterator,
   241  		)
   242  	}
   243  }
   244  
   245  func TestFile_Lines_Failure(t *testing.T) {
   246  	// Create a File instance with an invalid path for testing
   247  	file := g.NewFile(g.String("/invalid/path"))
   248  
   249  	// Read the file line by line
   250  	result := file.Lines()
   251  
   252  	// Check if the result is an error
   253  	if !result.IsErr() {
   254  		t.Fatalf(
   255  			"TestFile_Lines_Failure: Expected Lines to return an error, but got a successful result: %v",
   256  			result.Ok(),
   257  		)
   258  	}
   259  }
   260  
   261  func TestFile_Append_Success(t *testing.T) {
   262  	// Create a temporary file for testing
   263  	tempFile := createTempFile(t)
   264  	defer os.Remove(tempFile)
   265  
   266  	// Create a File instance representing the temporary file
   267  	file := g.NewFile(g.String(tempFile))
   268  
   269  	// Append content to the file
   270  	content := "appended content"
   271  	result := file.Append(g.String(content))
   272  
   273  	defer file.Close()
   274  
   275  	// Check if the result is successful
   276  	if result.IsErr() {
   277  		t.Fatalf(
   278  			"TestFile_Append_Success: Expected Append to return a successful result, but got an error: %v",
   279  			result.Err(),
   280  		)
   281  	}
   282  
   283  	// Read the content of the file to verify the appended content
   284  	fileContent, err := os.ReadFile(tempFile)
   285  	if err != nil {
   286  		t.Fatalf("TestFile_Append_Success: Failed to read file content: %v", err)
   287  	}
   288  
   289  	// Check if the appended content is present in the file
   290  	if string(fileContent) != content {
   291  		t.Errorf("TestFile_Append_Success: Expected file content to be %s, got %s", content, string(fileContent))
   292  	}
   293  }
   294  
   295  func TestFile_Append_Failure(t *testing.T) {
   296  	// Create a File instance with an invalid path for testing
   297  	file := g.NewFile(g.String("/invalid/path"))
   298  
   299  	// Append content to the file
   300  	result := file.Append(g.String("appended content"))
   301  
   302  	// Check if the result is an error
   303  	if !result.IsErr() {
   304  		t.Fatalf(
   305  			"TestFile_Append_Failure: Expected Append to return an error, but got a successful result: %v",
   306  			result.Ok(),
   307  		)
   308  	}
   309  }
   310  
   311  func TestFile_Seek_Success(t *testing.T) {
   312  	// Create a temporary file for testing
   313  	tempFile := createTempFile(t)
   314  	defer os.Remove(tempFile)
   315  
   316  	// Write content to the temporary file
   317  	writeToFile(t, tempFile, "test content")
   318  
   319  	// Create a File instance representing the temporary file
   320  	file := g.NewFile(g.String(tempFile))
   321  
   322  	// Seek to the middle of the file
   323  	result := file.Seek(5, io.SeekStart)
   324  
   325  	// Check if the result is successful
   326  	if result.IsErr() {
   327  		t.Fatalf(
   328  			"TestFile_Seek_Success: Expected Seek to return a successful result, but got an error: %v",
   329  			result.Err(),
   330  		)
   331  	}
   332  }
   333  
   334  func TestFile_Seek_Failure(t *testing.T) {
   335  	// Create a File instance with an invalid path for testing
   336  	file := g.NewFile(g.String("/invalid/path"))
   337  
   338  	// Seek to a position in the file
   339  	result := file.Seek(10, io.SeekStart)
   340  
   341  	// Check if the result is an error
   342  	if result.IsOk() {
   343  		t.Fatalf(
   344  			"TestFile_Seek_Failure: Expected Seek to return an error, but got a successful result: %v",
   345  			result.Ok(),
   346  		)
   347  	}
   348  
   349  	// Check if the error is of the correct type
   350  	if !os.IsNotExist(result.Err()) {
   351  		t.Fatalf(
   352  			"TestFile_Seek_Failure: Expected error to be of type os.ErrNotExist, but got: %v",
   353  			result.Err(),
   354  		)
   355  	}
   356  }
   357  
   358  func TestFile_Rename_Success(t *testing.T) {
   359  	// Create a temporary file for testing
   360  	tempFile := createTempFile(t)
   361  	defer os.Remove(tempFile)
   362  
   363  	// Create a File instance representing the temporary file
   364  	file := g.NewFile(g.String(tempFile))
   365  
   366  	// Define the new path for renaming
   367  	newPath := g.NewFile(g.String(tempFile) + "_renamed")
   368  	defer newPath.Remove()
   369  
   370  	// Rename the file
   371  	result := file.Rename(newPath.Path().Ok())
   372  
   373  	// Check if the result is successful
   374  	if result.IsErr() {
   375  		t.Fatalf(
   376  			"TestFile_Rename_Success: Expected Rename to return a successful result, but got an error: %v",
   377  			result.Err(),
   378  		)
   379  	}
   380  
   381  	// Verify if the file exists at the new path
   382  	if !newPath.Exist() {
   383  		t.Fatalf("TestFile_Rename_Success: File does not exist at the new path: %s", newPath.Path().Ok().Std())
   384  	}
   385  
   386  	// Verify if the original file does not exist
   387  	if file.Exist() {
   388  		t.Fatalf("TestFile_Rename_Success: Original file still exists after renaming: %s", file.Path().Ok().Std())
   389  	}
   390  }
   391  
   392  func TestFile_Rename_FileNotExist(t *testing.T) {
   393  	// Create a File instance with an invalid path for testing
   394  	file := g.NewFile(g.String("/invalid/path"))
   395  
   396  	// Define the new path for renaming
   397  	newPath := g.NewFile(g.String("/new/path"))
   398  
   399  	// Rename the file
   400  	result := file.Rename(newPath.Path().Ok())
   401  
   402  	// Check if the result is an error
   403  	if !result.IsErr() {
   404  		t.Fatalf(
   405  			"TestFile_Rename_FileNotExist: Expected Rename to return an error, but got a successful result: %v",
   406  			result.Ok(),
   407  		)
   408  	}
   409  
   410  	// Check if the error is of type ErrFileNotExist
   411  	_, ok := result.Err().(*g.ErrFileNotExist)
   412  	if !ok {
   413  		t.Fatalf("TestFile_Rename_FileNotExist: Expected error of type ErrFileNotExist, got: %v", result.Err())
   414  	}
   415  }
   416  
   417  func TestFile_OpenFile_ReadLock(t *testing.T) {
   418  	// Create a temporary file for testing
   419  	tempFile := createTempFile(t)
   420  	defer os.Remove(tempFile)
   421  
   422  	// Create a File instance representing the temporary file
   423  	file := g.NewFile(g.String(tempFile))
   424  
   425  	file.Guard()
   426  	defer file.Close()
   427  
   428  	// Open the file with read-lock
   429  	result := file.OpenFile(os.O_RDONLY, 0644)
   430  
   431  	// Check if the result is successful
   432  	if result.IsErr() {
   433  		t.Fatalf(
   434  			"TestFile_OpenFile_ReadLock: Expected OpenFile to return a successful result, but got an error: %v",
   435  			result.Err(),
   436  		)
   437  	}
   438  }
   439  
   440  func TestFile_OpenFile_WriteLock(t *testing.T) {
   441  	// Create a temporary file for testing
   442  	tempFile := createTempFile(t)
   443  	defer os.Remove(tempFile)
   444  
   445  	// Create a File instance representing the temporary file
   446  	file := g.NewFile(g.String(tempFile))
   447  
   448  	file.Guard()
   449  	defer file.Close()
   450  
   451  	// Open the file with write-lock
   452  	result := file.OpenFile(os.O_WRONLY, 0644)
   453  
   454  	// Check if the result is successful
   455  	if result.IsErr() {
   456  		t.Fatalf(
   457  			"TestFile_OpenFile_WriteLock: Expected OpenFile to return a successful result, but got an error: %v",
   458  			result.Err(),
   459  		)
   460  	}
   461  }
   462  
   463  func TestFile_OpenFile_Truncate(t *testing.T) {
   464  	// Create a temporary file for testing
   465  	tempFile := createTempFile(t)
   466  	defer os.Remove(tempFile)
   467  
   468  	// Create a File instance representing the temporary file
   469  	file := g.NewFile(g.String(tempFile))
   470  
   471  	file.Guard()
   472  	defer file.Close()
   473  
   474  	// Open the file with truncation flag
   475  	result := file.OpenFile(os.O_WRONLY|os.O_TRUNC, 0644)
   476  
   477  	// Check if the result is successful
   478  	if result.IsErr() {
   479  		t.Fatalf(
   480  			"TestFile_OpenFile_Truncate: Expected OpenFile to return a successful result, but got an error: %v",
   481  			result.Err(),
   482  		)
   483  	}
   484  
   485  	// Verify if the file is truncated
   486  	fileStat, err := os.Stat(tempFile)
   487  	if err != nil {
   488  		t.Fatalf("TestFile_OpenFile_Truncate: Failed to get file stat: %v", err)
   489  	}
   490  	if fileStat.Size() != 0 {
   491  		t.Fatalf("TestFile_OpenFile_Truncate: File is not truncated")
   492  	}
   493  }
   494  
   495  func TestFile_Chmod(t *testing.T) {
   496  	// Create a temporary file for testing
   497  	tempFile := createTempFile(t)
   498  	defer os.Remove(tempFile)
   499  
   500  	// Create a File instance representing the temporary file
   501  	file := g.NewFile(g.String(tempFile))
   502  
   503  	// Change the mode of the file
   504  	result := file.Chmod(0644)
   505  
   506  	// Check if the result is successful
   507  	if result.IsErr() {
   508  		t.Fatalf("TestFile_Chmod: Expected Chmod to return a successful result, but got an error: %v", result.Err())
   509  	}
   510  
   511  	// Verify if the mode of the file is changed
   512  	fileStat, err := os.Stat(tempFile)
   513  	if err != nil {
   514  		t.Fatalf("TestFile_Chmod: Failed to get file stat: %v", err)
   515  	}
   516  	expectedMode := os.FileMode(0644)
   517  	if fileStat.Mode() != expectedMode {
   518  		t.Fatalf("TestFile_Chmod: Expected file mode to be %v, but got %v", expectedMode, fileStat.Mode())
   519  	}
   520  }
   521  
   522  func TestFile_Chown(t *testing.T) {
   523  	// Create a temporary file for testing
   524  	tempFile := createTempFile(t)
   525  	defer os.Remove(tempFile)
   526  
   527  	// Create a File instance representing the temporary file
   528  	file := g.NewFile(g.String(tempFile))
   529  
   530  	// Change the owner of the file
   531  	result := file.Chown(os.Getuid(), os.Getgid())
   532  
   533  	// Check if the result is successful
   534  	if result.IsErr() {
   535  		t.Fatalf("TestFile_Chown: Expected Chown to return a successful result, but got an error: %v", result.Err())
   536  	}
   537  
   538  	// Verify if the owner of the file is changed
   539  	fileStat, err := os.Stat(tempFile)
   540  	if err != nil {
   541  		t.Fatalf("TestFile_Chown: Failed to get file stat: %v", err)
   542  	}
   543  	expectedUID := os.Getuid()
   544  	expectedGID := os.Getgid()
   545  	statT, ok := fileStat.Sys().(*syscall.Stat_t)
   546  	if !ok {
   547  		t.Fatalf("TestFile_Chown: Failed to get file Sys info")
   548  	}
   549  	if int(statT.Uid) != expectedUID || int(statT.Gid) != expectedGID {
   550  		t.Fatalf(
   551  			"TestFile_Chown: Expected file owner to be UID: %d, GID: %d, but got UID: %d, GID: %d",
   552  			expectedUID,
   553  			expectedGID,
   554  			statT.Uid,
   555  			statT.Gid,
   556  		)
   557  	}
   558  }
   559  
   560  func TestFile_WriteFromReader(t *testing.T) {
   561  	// Create a temporary file for testing
   562  	tempFile := createTempFile(t)
   563  	defer os.Remove(tempFile)
   564  
   565  	// Create a File instance representing the temporary file
   566  	file := g.NewFile(g.String(tempFile))
   567  
   568  	// Prepare data to write
   569  	testData := "Hello, World!"
   570  	reader := bytes.NewBufferString(testData)
   571  
   572  	// Write data from the reader into the file
   573  	result := file.WriteFromReader(reader)
   574  
   575  	// Check if the result is successful
   576  	if result.IsErr() {
   577  		t.Fatalf(
   578  			"TestFile_WriteFromReader: Expected WriteFromReader to return a successful result, but got an error: %v",
   579  			result.Err(),
   580  		)
   581  	}
   582  
   583  	// Read the content of the file to verify if the data is written correctly
   584  	contentResult := file.Read()
   585  	if contentResult.IsErr() {
   586  		t.Fatalf("TestFile_WriteFromReader: Failed to read file content: %v", contentResult.Err())
   587  	}
   588  
   589  	// Verify if the content of the file matches the test data
   590  	if contentResult.Ok().Std() != testData {
   591  		t.Fatalf(
   592  			"TestFile_WriteFromReader: Expected file content to be '%s', but got '%s'",
   593  			testData,
   594  			contentResult.Ok().Std(),
   595  		)
   596  	}
   597  }
   598  
   599  func TestFile_Write(t *testing.T) {
   600  	// Create a temporary file for testing
   601  	tempFile := createTempFile(t)
   602  	defer os.Remove(tempFile)
   603  
   604  	// Create a File instance representing the temporary file
   605  	file := g.NewFile(g.String(tempFile))
   606  
   607  	// Prepare data to write
   608  	testData := "Hello, World!"
   609  
   610  	// Write data into the file
   611  	result := file.Write(g.String(testData))
   612  
   613  	// Check if the result is successful
   614  	if result.IsErr() {
   615  		t.Fatalf("TestFile_Write: Expected Write to return a successful result, but got an error: %v", result.Err())
   616  	}
   617  
   618  	// Read the content of the file to verify if the data is written correctly
   619  	contentResult := file.Read()
   620  	if contentResult.IsErr() {
   621  		t.Fatalf("TestFile_Write: Failed to read file content: %v", contentResult.Err())
   622  	}
   623  
   624  	// Verify if the content of the file matches the test data
   625  	if contentResult.Ok().Std() != testData {
   626  		t.Fatalf("TestFile_Write: Expected file content to be '%s', but got '%s'", testData, contentResult.Ok().Std())
   627  	}
   628  }
   629  
   630  func TestFile_Ext(t *testing.T) {
   631  	// Create a temporary file for testing
   632  	tempFile := createTempFile(t)
   633  	defer os.Remove(tempFile)
   634  
   635  	// Create a File instance representing the temporary file
   636  	file := g.NewFile(g.String(tempFile))
   637  
   638  	// Extract the file extension
   639  	extension := file.Ext().Std()
   640  
   641  	// Expected file extension (assuming the temporary file has an extension)
   642  	expectedExtension := ".txt"
   643  
   644  	// Check if the extracted extension matches the expected extension
   645  	if extension != expectedExtension {
   646  		t.Fatalf("TestFile_Ext: Expected extension to be '%s', but got '%s'", expectedExtension, extension)
   647  	}
   648  }
   649  
   650  func TestFile_Copy(t *testing.T) {
   651  	// Create a temporary source file for testing
   652  	srcFile := createTempFile(t)
   653  	defer os.Remove(srcFile)
   654  
   655  	// Create a temporary destination file for testing
   656  	destFile := createTempFile(t)
   657  	defer os.Remove(destFile)
   658  
   659  	// Create a File instance representing the source file
   660  	src := g.NewFile(g.String(srcFile))
   661  
   662  	// Copy the source file to the destination file
   663  	dest := g.NewFile(g.String(destFile))
   664  	result := src.Copy(dest.Path().Ok())
   665  
   666  	// Check if the copy operation was successful
   667  	if result.IsErr() {
   668  		t.Fatalf("TestFile_Copy: Failed to copy file: %s", result.Err())
   669  	}
   670  
   671  	// Verify that the destination file exists
   672  	if !dest.Exist() {
   673  		t.Fatalf("TestFile_Copy: Destination file does not exist after copy")
   674  	}
   675  }
   676  
   677  func TestFile_Split(t *testing.T) {
   678  	// Create a temporary file for testing
   679  	tempFile := createTempFile(t)
   680  	defer os.Remove(tempFile)
   681  
   682  	// Create a File instance representing the temporary file
   683  	file := g.NewFile(g.String(tempFile))
   684  
   685  	// Split the file path into its directory and file components
   686  	dir, fileName := file.Split()
   687  
   688  	// Check if the directory and file components are correct
   689  	if dir == nil || dir.Path().Ok().Std() != filepath.Dir(tempFile) {
   690  		t.Errorf("TestFile_Split: Incorrect directory component")
   691  	}
   692  
   693  	if fileName == nil || fileName.Name().Std() != filepath.Base(tempFile) {
   694  		t.Errorf("TestFile_Split: Incorrect file name component")
   695  	}
   696  }
   697  
   698  func TestFile_Move(t *testing.T) {
   699  	// Create a temporary file for testing
   700  	tempFile := createTempFile(t)
   701  	defer os.Remove(tempFile)
   702  
   703  	// Create a File instance representing the temporary file
   704  	file := g.NewFile(g.String(tempFile))
   705  
   706  	// Rename the file
   707  	newpath := tempFile + ".new"
   708  	renamedFile := file.Move(g.String(newpath))
   709  
   710  	// Check if the file has been successfully renamed
   711  	if renamedFile.IsErr() {
   712  		t.Errorf("TestFile_Rename: Failed to rename file: %v", renamedFile.Err())
   713  	}
   714  
   715  	defer renamedFile.Ok().Remove()
   716  
   717  	// Verify that the old file does not exist
   718  	if _, err := os.Stat(tempFile); !os.IsNotExist(err) {
   719  		t.Errorf("TestFile_Rename: Old file still exists after renaming")
   720  	}
   721  
   722  	// Verify that the new file exists
   723  	if _, err := os.Stat(newpath); os.IsNotExist(err) {
   724  		t.Errorf("TestFile_Rename: New file does not exist after renaming")
   725  	}
   726  }
   727  
   728  // writeToFile writes content to the specified file.
   729  func writeToFile(t *testing.T, filename, content string) {
   730  	file, err := os.OpenFile(filename, os.O_WRONLY, 0644)
   731  	if err != nil {
   732  		t.Fatalf("Failed to open file %s for writing: %s", filename, err)
   733  	}
   734  	defer file.Close()
   735  
   736  	_, err = io.Copy(file, strings.NewReader(content))
   737  	if err != nil {
   738  		t.Fatalf("Failed to write content to file %s: %s", filename, err)
   739  	}
   740  }
   741  
   742  // createTempFile creates a temporary file for testing and returns its path.
   743  func createTempFile(t *testing.T) string {
   744  	tempFile, err := os.CreateTemp("", "testfile*.txt")
   745  	if err != nil {
   746  		t.Fatalf("Failed to create temporary file: %s", err)
   747  	}
   748  
   749  	defer tempFile.Close()
   750  
   751  	return tempFile.Name()
   752  }