github.com/alibabacloud-go/tea@v1.3.10/dara/file_test.go (about)

     1  package dara
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  // TestNewDaraFile tests the NewDaraFile function
    12  func TestNewDaraFile(t *testing.T) {
    13  	path := "testfile.txt"
    14  	tf := NewDaraFile(path)
    15  	if tf.Path() != path {
    16  		t.Errorf("Expected path to be %s, got %s", path, tf.Path())
    17  	}
    18  }
    19  
    20  // TestCreateTime tests the CreateTime method
    21  func TestCreateTime(t *testing.T) {
    22  	path := "testfile.txt"
    23  	ioutil.WriteFile(path, []byte("test"), 0644) // 创建文件以确保它存在
    24  	defer os.Remove(path)
    25  
    26  	tf := NewDaraFile(path)
    27  	date, err := tf.CreateTime()
    28  	if err != nil {
    29  		t.Fatalf("unexpected error: %v", err)
    30  	}
    31  	if date == nil {
    32  		t.Error("expected a valid TeaDate, got nil")
    33  	}
    34  }
    35  
    36  // TestModifyTime tests the ModifyTime method
    37  func TestModifyTime(t *testing.T) {
    38  	path := "testfile.txt"
    39  	ioutil.WriteFile(path, []byte("test"), 0644)
    40  	defer os.Remove(path)
    41  
    42  	tf := NewDaraFile(path)
    43  	date, err := tf.ModifyTime()
    44  	if err != nil {
    45  		t.Fatalf("unexpected error: %v", err)
    46  	}
    47  	if date == nil {
    48  		t.Error("expected a valid TeaDate, got nil")
    49  	}
    50  }
    51  
    52  // TestLength tests the Length method
    53  func TestLength(t *testing.T) {
    54  	path := "testfile.txt"
    55  	content := []byte("Hello, World!")
    56  	ioutil.WriteFile(path, content, 0644)
    57  	defer os.Remove(path)
    58  
    59  	tf := NewDaraFile(path)
    60  	length, err := tf.Length()
    61  	if err != nil {
    62  		t.Fatalf("unexpected error: %v", err)
    63  	}
    64  	if length != int64(len(content)) {
    65  		t.Errorf("expected length %d, got %d", len(content), length)
    66  	}
    67  }
    68  
    69  // TestRead tests the Read method
    70  func TestRead(t *testing.T) {
    71  	path := "testfile.txt"
    72  	content := []byte("Hello, World!")
    73  	ioutil.WriteFile(path, content, 0644)
    74  	defer os.Remove(path)
    75  
    76  	tf := NewDaraFile(path)
    77  	data, err := tf.Read(5)
    78  	if err != nil {
    79  		t.Fatalf("unexpected error: %v", err)
    80  	}
    81  	if string(data) != "Hello" {
    82  		t.Errorf("expected 'Hello', got '%s'", string(data))
    83  	}
    84  
    85  	// Read the rest of the file
    86  	data, err = tf.Read(10)
    87  	if err != nil {
    88  		t.Fatalf("unexpected error: %v", err)
    89  	}
    90  	if string(data) != ", World!" {
    91  		t.Errorf("expected ', World!', got '%s'", string(data))
    92  	}
    93  }
    94  
    95  // TestWrite tests the Write method
    96  func TestWrite(t *testing.T) {
    97  	path := "testfile.txt"
    98  	tf := NewDaraFile(path)
    99  
   100  	data := []byte("Hello, Write Test!")
   101  	err := tf.Write(data)
   102  	if err != nil {
   103  		t.Fatalf("unexpected error: %v", err)
   104  	}
   105  
   106  	// Validate the content of the file
   107  	readData, _ := ioutil.ReadFile(path)
   108  	if string(readData) != "Hello, Write Test!" {
   109  		t.Errorf("expected file content to be %s, got %s", string(data), string(readData))
   110  	}
   111  }
   112  
   113  // TestClose tests the Close method
   114  func TestClose(t *testing.T) {
   115  	path := "testfile.txt"
   116  	tf := NewDaraFile(path)
   117  	err := tf.Close()
   118  	if err != nil {
   119  		t.Fatalf("unexpected error: %v", err)
   120  	}
   121  }
   122  
   123  // TestExists tests the Exists function
   124  func TestExists(t *testing.T) {
   125  	path := "testfile.txt"
   126  	ioutil.WriteFile(path, []byte("test"), 0644)
   127  	defer os.Remove(path)
   128  
   129  	exists, err := Exists(path)
   130  	if err != nil || !exists {
   131  		t.Errorf("expected file to exist, got error: %v", err)
   132  	}
   133  
   134  	exists, err = Exists("nonexistent.txt")
   135  	if err != nil || exists {
   136  		t.Errorf("expected file to not exist, got error: %v", err)
   137  	}
   138  }
   139  
   140  func TestCreateReadWriteStream(t *testing.T) {
   141  	tempDir, err := ioutil.TempDir("", "example")
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	defer os.RemoveAll(tempDir)
   146  	testFile := filepath.Join(tempDir, "test.txt")
   147  	testWFile := filepath.Join(tempDir, "test2.txt")
   148  
   149  	// Prepare the test file
   150  	originalContent := "Hello, World!"
   151  	if err := ioutil.WriteFile(testFile, []byte(originalContent), 0644); err != nil {
   152  		t.Fatalf("failed to write test file: %v", err)
   153  	}
   154  
   155  	// Test CreateReadStream
   156  	rs, err := CreateReadStream(testFile)
   157  	if err != nil {
   158  		t.Fatalf("failed to create read stream: %v", err)
   159  	}
   160  	defer rs.Close()
   161  
   162  	// Test CreateWriteStream
   163  	ws, err := CreateWriteStream(testWFile)
   164  	if err != nil {
   165  		t.Fatalf("failed to create write stream: %v", err)
   166  	}
   167  	defer ws.Close()
   168  
   169  	// Pipe data from read stream to write stream
   170  	if _, err := io.Copy(ws, rs); err != nil {
   171  		t.Fatalf("failed to copy data from read stream to write stream: %v", err)
   172  	}
   173  
   174  	// Read back the content to check if it's correct
   175  	data, err := ioutil.ReadFile(testWFile)
   176  	if err != nil {
   177  		t.Fatalf("failed to read back test file: %v", err)
   178  	}
   179  
   180  	if string(data) != originalContent {
   181  		t.Fatalf("expected %q but got %q", originalContent, string(data))
   182  	}
   183  }