github.com/enetx/g@v1.0.80/tests/dir_test.go (about) 1 package g_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/enetx/g" 9 ) 10 11 func TestNewDir(t *testing.T) { 12 // Define a test path 13 testPath := g.String("/") 14 15 // Create a new Dir instance using NewDir 16 dir := g.NewDir(testPath) 17 18 // Check if the path of the created Dir instance matches the expected path 19 if dir.Path().Ok() != testPath { 20 t.Errorf("TestNewDir: Expected path %s, got %s", testPath, dir.Path().Ok()) 21 } 22 } 23 24 func TestDir_Chown_Success(t *testing.T) { 25 // Create a temporary directory for testing 26 tempDir := createTempDir(t) 27 defer os.RemoveAll(tempDir) 28 29 // Create a Dir instance with the temporary directory path 30 dir := g.NewDir(g.String(tempDir)) 31 32 // Perform chown operation 33 uid := os.Getuid() // Current user's UID 34 gid := os.Getgid() // Current user's GID 35 result := dir.Chown(uid, gid) 36 37 // Check if the operation succeeded 38 if result.IsErr() { 39 t.Errorf("TestDir_Chown_Success: Unexpected error: %s", result.Err().Error()) 40 } 41 } 42 43 func TestDir_Chown_Failure(t *testing.T) { 44 // Create a Dir instance with a non-existent directory path 45 dir := g.NewDir("/nonexistent/path") 46 47 // Perform chown operation 48 uid := os.Getuid() // Current user's UID 49 gid := os.Getgid() // Current user's GID 50 result := dir.Chown(uid, gid) 51 52 // Check if the operation failed as expected 53 if result.IsOk() { 54 t.Errorf("TestDir_Chown_Failure: Expected error, got success") 55 } 56 } 57 58 func TestDir_Stat_Success(t *testing.T) { 59 // Create a temporary directory for testing 60 tempDir := createTempDir(t) 61 defer os.RemoveAll(tempDir) 62 63 // Create a Dir instance with the temporary directory path 64 dir := g.NewDir(g.String(tempDir)) 65 66 // Get directory information using Stat 67 result := dir.Stat() 68 69 // Check if the operation succeeded 70 if result.IsErr() { 71 t.Errorf("TestDir_Stat_Success: Unexpected error: %s", result.Err().Error()) 72 } 73 } 74 75 func TestDir_Stat_Failure(t *testing.T) { 76 // Create a Dir instance with a non-existent directory path 77 dir := g.NewDir("/nonexistent/path") 78 79 // Get directory information using Stat 80 result := dir.Stat() 81 82 // Check if the operation failed as expected 83 if result.IsOk() { 84 t.Errorf("TestDir_Stat_Failure: Expected error, got success") 85 } 86 } 87 88 func TestDir_Path_Success(t *testing.T) { 89 // Create a temporary directory for testing 90 tempDir := createTempDir(t) 91 defer os.RemoveAll(tempDir) 92 93 // Create a Dir instance with the temporary directory path 94 dir := g.NewDir(g.String(tempDir)) 95 96 // Get the absolute path of the directory using Path 97 result := dir.Path() 98 99 // Check if the operation succeeded 100 if result.IsErr() { 101 t.Errorf("TestDir_Path_Success: Unexpected error: %s", result.Err().Error()) 102 } 103 } 104 105 func TestDir_Lstat_IsLink_Success(t *testing.T) { 106 // Create a temporary directory for testing 107 tempDir := createTempDir(t) 108 defer os.RemoveAll(tempDir) 109 110 // Create a symbolic link to the temporary directory 111 linkPath := tempDir + "/link" 112 err := os.Symlink(tempDir, linkPath) 113 if err != nil { 114 t.Fatalf("Failed to create symbolic link: %s", err) 115 } 116 117 // Create a Dir instance with the symbolic link path 118 dir := g.NewDir(g.String(linkPath)) 119 120 // Call Lstat to get information about the symbolic link 121 result := dir.Lstat() 122 123 // Check if the operation succeeded 124 if result.IsErr() { 125 t.Errorf("TestDir_Lstat_IsLink_Success: Unexpected error: %s", result.Err().Error()) 126 } 127 128 // Check if IsLink correctly identifies the symbolic link 129 if !dir.IsLink() { 130 t.Errorf("TestDir_Lstat_IsLink_Success: Expected directory to be a symbolic link") 131 } 132 } 133 134 func TestDir_Lstat_IsLink_NotLink(t *testing.T) { 135 // Create a temporary directory for testing 136 tempDir := createTempDir(t) 137 defer os.RemoveAll(tempDir) 138 139 // Create a Dir instance with the temporary directory path 140 dir := g.NewDir(g.String(tempDir)) 141 142 // Call Lstat to get information about the directory 143 result := dir.Lstat() 144 145 // Check if the operation succeeded 146 if result.IsErr() { 147 t.Errorf("TestDir_Lstat_IsLink_NotLink: Unexpected error: %s", result.Err().Error()) 148 } 149 150 // Check if IsLink correctly identifies that it's not a symbolic link 151 if dir.IsLink() { 152 t.Errorf("TestDir_Lstat_IsLink_NotLink: Expected directory not to be a symbolic link") 153 } 154 } 155 156 func TestDir_CreateTemp_Success(t *testing.T) { 157 // Create a Dir instance representing the default directory for temporary directories 158 dir := g.NewDir("") 159 160 // Create a temporary directory using CreateTemp 161 result := dir.CreateTemp() 162 163 // Check if the operation succeeded 164 if result.IsErr() { 165 t.Errorf("TestDir_CreateTemp_Success: Unexpected error: %s", result.Err().Error()) 166 } 167 168 // Check if the temporary directory exists 169 tmpDir := result.Ok().Path().Ok().Std() 170 if _, err := os.Stat(tmpDir); os.IsNotExist(err) { 171 t.Errorf("TestDir_CreateTemp_Success: Temporary directory not created") 172 } 173 } 174 175 func TestDir_Temp(t *testing.T) { 176 // Get the default temporary directory using Temp 177 tmpDir := g.NewDir("").Temp() 178 179 // Check if the returned directory exists 180 if _, err := os.Stat(tmpDir.ToString().Std()); os.IsNotExist(err) { 181 t.Errorf("TestDir_Temp: Temporary directory does not exist") 182 } 183 } 184 185 func TestDir_Remove_Success(t *testing.T) { 186 // Create a temporary directory for testing 187 tempDir := createTempDir(t) 188 defer os.RemoveAll(tempDir) 189 190 // Create a Dir instance with the temporary directory path 191 dir := g.NewDir(g.String(tempDir)) 192 193 // Remove the temporary directory using Remove 194 result := dir.Remove() 195 196 // Check if the operation succeeded 197 if result.IsErr() { 198 t.Errorf("TestDir_Remove_Success: Unexpected error: %s", result.Err().Error()) 199 } 200 } 201 202 func TestDir_Remove_NotExist(t *testing.T) { 203 // Create a Dir instance with a non-existent directory path 204 dir := g.NewDir("/nonexistent/path") 205 206 // Remove the non-existent directory using Remove 207 result := dir.Remove() 208 209 // Check if the operation succeeded (non-existent directory should be considered removed) 210 if result.IsErr() { 211 t.Errorf("TestDir_Remove_NotExist: Unexpected error: %s", result.Err().Error()) 212 } 213 } 214 215 func TestDir_Create_Success(t *testing.T) { 216 // Create a temporary directory for testing 217 tempDir := createTempDir(t) 218 os.RemoveAll(tempDir) 219 220 defer os.RemoveAll(tempDir) 221 222 // Create a Dir instance with the temporary directory path 223 dir := g.NewDir(g.String(tempDir)) 224 225 // Create a new directory using Create 226 result := dir.Create() 227 228 // Check if the operation succeeded 229 if result.IsErr() { 230 t.Errorf("TestDir_Create_Success: Unexpected error: %s", result.Err().Error()) 231 } 232 233 // Check if the created directory exists 234 createdDir := dir.Path().Ok().Std() 235 if _, err := os.Stat(createdDir); os.IsNotExist(err) { 236 t.Errorf("TestDir_Create_Success: Created directory does not exist") 237 } 238 } 239 240 func TestDir_Create_Failure(t *testing.T) { 241 // Attempt to create a directory in a non-existent parent directory 242 nonExistentDir := g.NewDir("/nonexistent/parent") 243 result := nonExistentDir.Create() 244 245 // Check if the operation failed as expected 246 if result.IsOk() { 247 t.Errorf("TestDir_Create_Failure: Expected error, got success") 248 } 249 } 250 251 func TestDir_Join_Success(t *testing.T) { 252 // Create a Dir instance representing an existing directory 253 dir := g.NewDir("/path/to/directory") 254 255 // Join the directory path with additional elements 256 result := dir.Join("subdir", "file.txt") 257 258 // Check if the operation succeeded 259 if result.IsErr() { 260 t.Errorf("TestDir_Join_Success: Unexpected error: %s", result.Err().Error()) 261 } 262 263 // Check if the joined path matches the expected value 264 expectedPath := "/path/to/directory/subdir/file.txt" 265 if result.Ok().Std() != expectedPath { 266 t.Errorf("TestDir_Join_Success: Expected joined path '%s', got '%s'", expectedPath, result.Ok().Std()) 267 } 268 } 269 270 func TestDir_SetPath(t *testing.T) { 271 // Create a Dir instance representing an existing directory 272 dir := g.NewDir("/path/to/directory") 273 274 // Set a new path for the directory 275 newPath := g.String("/new/path/to/directory") 276 updatedDir := dir.SetPath(newPath) 277 278 // Check if the path of the directory is updated correctly 279 if updatedDir.Path().Ok() != newPath { 280 t.Errorf("TestDir_SetPath: Expected path '%s', got '%s'", newPath, updatedDir.Path().Ok()) 281 } 282 } 283 284 func TestDir_CreateAll_Success(t *testing.T) { 285 // Create a temporary directory for testing 286 tempDir := createTempDir(t) 287 os.RemoveAll(tempDir) 288 defer os.RemoveAll(tempDir) 289 290 // Create a Dir instance representing the temporary directory 291 dir := g.NewDir(g.String(tempDir)) 292 293 // Create all directories along the path 294 result := dir.CreateAll() 295 296 // Check if the operation succeeded 297 if result.IsErr() { 298 t.Errorf("TestDir_CreateAll_Success: Unexpected error: %s", result.Err().Error()) 299 } 300 301 // Check if the directories along the path are created 302 if _, err := os.Stat(tempDir); os.IsNotExist(err) { 303 t.Errorf("TestDir_CreateAll_Success: Directory does not exist: %s", tempDir) 304 } 305 } 306 307 func TestDir_CreateAll_Mode_Success(t *testing.T) { 308 // Create a temporary directory for testing 309 tempDir := createTempDir(t) 310 os.RemoveAll(tempDir) 311 defer os.RemoveAll(tempDir) 312 313 // Create a Dir instance representing the temporary directory 314 dir := g.NewDir(g.String(tempDir)) 315 316 // Create all directories along the path with custom mode 317 result := dir.CreateAll(0700) 318 319 // Check if the operation succeeded 320 if result.IsErr() { 321 t.Errorf("TestDir_CreateAll_Mode_Success: Unexpected error: %s", result.Err().Error()) 322 } 323 324 // Check if the directories along the path are created with the specified mode 325 fileInfo, err := os.Stat(tempDir) 326 if os.IsNotExist(err) { 327 t.Errorf("TestDir_CreateAll_Mode_Success: Directory does not exist: %s", tempDir) 328 } else { 329 if fileInfo.Mode() != os.FileMode(0700)|os.ModeDir { 330 t.Errorf("TestDir_CreateAll_Mode_Success: Expected mode 0700, got %o", fileInfo.Mode()) 331 } 332 } 333 } 334 335 func TestDir_Read_Success(t *testing.T) { 336 // Create a temporary directory for testing 337 tempDir := createTempDir(t) 338 defer os.RemoveAll(tempDir) 339 340 // Create some files and directories inside the temporary directory 341 createTestFiles(tempDir) 342 343 // Create a Dir instance representing the temporary directory 344 dir := g.NewDir(g.String(tempDir)) 345 346 // Read the content of the directory 347 result := dir.Read() 348 349 // Check if the operation succeeded 350 if result.IsErr() { 351 t.Errorf("TestDir_Read_Success: Unexpected error: %s", result.Err().Error()) 352 } 353 354 // Check if the returned slice of File instances is accurate 355 files := result.Ok() 356 expectedFileNames := []string{"file1.txt", "file2.txt", "subdir1", "subdir2"} 357 for i, file := range files { 358 if file.Name().Std() != expectedFileNames[i] { 359 t.Errorf("TestDir_Read_Success: Expected file '%s', got '%s'", expectedFileNames[i], file.Name().Std()) 360 } 361 } 362 } 363 364 func TestDir_Glob_Success(t *testing.T) { 365 // Create a temporary directory for testing 366 tempDir := createTempDir(t) 367 defer os.RemoveAll(tempDir) 368 369 // Create some test files inside the temporary directory 370 createTestFiles(tempDir) 371 372 // Create a Dir instance representing the temporary directory with a glob pattern 373 dir := g.NewDir(g.String(filepath.Join(tempDir, "*.txt"))) 374 375 // Retrieve files matching the glob pattern 376 result := dir.Glob() 377 378 // Check if the operation succeeded 379 if result.IsErr() { 380 t.Errorf("TestDir_Glob_Success: Unexpected error: %s", result.Err().Error()) 381 } 382 383 // Check if the returned slice of File instances is accurate 384 files := result.Ok() 385 expectedFileNames := []string{"file1.txt", "file2.txt"} 386 for i, file := range files { 387 if file.Name().Std() != expectedFileNames[i] { 388 t.Errorf("TestDir_Glob_Success: Expected file '%s', got '%s'", expectedFileNames[i], file.Name().Std()) 389 } 390 } 391 } 392 393 func TestDir_Rename(t *testing.T) { 394 // Create a temporary directory for testing 395 tempDir := createTempDir(t) 396 defer os.RemoveAll(tempDir) 397 398 // Create a Dir instance representing the temporary directory 399 dir := g.NewDir(g.String(tempDir)) 400 401 // Rename the directory 402 newpath := tempDir + "_new" 403 renamedDir := dir.Rename(g.String(newpath)) 404 405 // Check if the directory has been successfully renamed 406 if renamedDir.IsErr() { 407 t.Errorf("TestDir_Rename: Failed to rename directory: %v", renamedDir.Err()) 408 } 409 410 defer renamedDir.Ok().Remove() 411 412 // Verify that the old directory does not exist 413 if _, err := os.Stat(tempDir); !os.IsNotExist(err) { 414 t.Errorf("TestDir_Rename: Old directory still exists after renaming") 415 } 416 417 // Verify that the new directory exists 418 if _, err := os.Stat(newpath); os.IsNotExist(err) { 419 t.Errorf("TestDir_Rename: New directory does not exist after renaming") 420 } 421 } 422 423 func TestDir_Copy(t *testing.T) { 424 // Create a temporary source directory for testing 425 sourceDir := createTempDir(t) 426 defer os.RemoveAll(sourceDir) 427 428 // Create some test files in the source directory 429 if err := os.WriteFile(sourceDir+"/file1.txt", []byte("File 1 content"), 0644); err != nil { 430 t.Fatalf("TestDir_Copy: Failed to create test file 1 in source directory: %v", err) 431 } 432 if err := os.WriteFile(sourceDir+"/file2.txt", []byte("File 2 content"), 0644); err != nil { 433 t.Fatalf("TestDir_Copy: Failed to create test file 2 in source directory: %v", err) 434 } 435 436 // Create a temporary destination directory for testing 437 destDir := createTempDir(t) 438 defer os.RemoveAll(destDir) 439 440 // Create a Dir instance representing the source directory 441 source := g.NewDir(g.String(sourceDir)) 442 443 // Copy the contents of the source directory to the destination directory 444 result := source.Copy(g.String(destDir)) 445 if result.IsErr() { 446 t.Fatalf("TestDir_Copy: Failed to copy directory contents: %v", result.Err()) 447 } 448 449 // Verify that the destination directory contains the same files as the source directory 450 destFiles, err := os.ReadDir(destDir) 451 if err != nil { 452 t.Fatalf("TestDir_Copy: Failed to read destination directory: %v", err) 453 } 454 455 expectedFiles := []string{"file1.txt", "file2.txt"} 456 for _, expectedFile := range expectedFiles { 457 found := false 458 for _, destFile := range destFiles { 459 if destFile.Name() == expectedFile { 460 found = true 461 break 462 } 463 } 464 if !found { 465 t.Errorf("TestDir_Copy: Destination directory missing file %s", expectedFile) 466 } 467 } 468 } 469 470 func TestDir_Walk(t *testing.T) { 471 // Create a temporary directory for testing 472 testDir := createTempDir(t) 473 defer os.RemoveAll(testDir) 474 475 // Create some test files and directories within the test directory 476 if err := os.WriteFile(testDir+"/file1.txt", []byte("File 1 content"), 0644); err != nil { 477 t.Fatalf("TestDir_Walk: Failed to create test file 1: %v", err) 478 } 479 480 if err := os.Mkdir(testDir+"/subdir", 0755); err != nil { 481 t.Fatalf("TestDir_Walk: Failed to create test directory: %v", err) 482 } 483 484 if err := os.WriteFile(testDir+"/subdir/file2.txt", []byte("File 2 content"), 0644); err != nil { 485 t.Fatalf("TestDir_Walk: Failed to create test file 2: %v", err) 486 } 487 488 if err := os.WriteFile(testDir+"/subdir/file2.txt", []byte("File 2 content"), 0644); err != nil { 489 t.Fatalf("TestDir_Walk: Failed to create test file 2: %v", err) 490 } 491 492 if err := os.Symlink(testDir, testDir+"/link"); err != nil { 493 t.Fatalf("Failed to create symbolic link: %s", err) 494 } 495 496 // Define a slice to store the paths of visited files and directories 497 visited := make([]string, 0) 498 499 // Define the walker function 500 walker := func(f *g.File) error { 501 path := f.Path() 502 if path.IsErr() { 503 return path.Err() 504 } 505 506 if f.IsDir() && f.Dir().Ok().IsLink() { 507 return g.SkipWalk 508 } 509 510 if f.IsLink() { 511 return nil 512 } 513 514 visited = append(visited, path.Ok().Std()) 515 return nil 516 } 517 518 // Create a Dir instance representing the test directory 519 testDirInstance := g.NewDir(g.String(testDir)) 520 521 // Perform the walk operation 522 if err := testDirInstance.Walk(walker); err != nil { 523 t.Fatalf("TestDir_Walk: Walk operation failed: %v", err) 524 } 525 526 // Verify that the walker function was applied to all files and directories 527 expectedPaths := []string{testDir + "/file1.txt", testDir + "/subdir", testDir + "/subdir/file2.txt"} 528 for _, expectedPath := range expectedPaths { 529 found := false 530 for _, v := range visited { 531 if v == expectedPath { 532 found = true 533 break 534 } 535 } 536 if !found { 537 t.Errorf("TestDir_Walk: Expected path not visited: %s", expectedPath) 538 } 539 } 540 } 541 542 func createTestFiles(dir string) { 543 // Create some test files and directories inside the provided directory 544 os.Mkdir(filepath.Join(dir, "subdir1"), 0755) 545 os.Mkdir(filepath.Join(dir, "subdir2"), 0755) 546 os.Create(filepath.Join(dir, "file1.txt")) 547 os.Create(filepath.Join(dir, "file2.txt")) 548 } 549 550 // createTempDir creates a temporary directory for testing and returns its path. 551 func createTempDir(t *testing.T) string { 552 tempDir, err := os.MkdirTemp("", "testdir") 553 if err != nil { 554 t.Fatalf("Failed to create temporary directory: %s", err) 555 } 556 return tempDir 557 }