func()

in registry/storage/driver/testsuites/testsuites.go [2421:2529]


func (s *DriverSuite) TestWalkSkipDir() {
	rootDirectory := "/" + dtestutil.RandomFilenameRange(8, 8)
	defer s.deletePath(s.StorageDriver, rootDirectory)
	s.T().Logf("root directory used for testing: %s", rootDirectory)

	// Create directories with a structure like:
	// rootDirectory/
	//   ├── dir1/
	//   │    ├── file1-1
	//   │    └── file1-2
	//   ├── dir2/  (this one will be skipped)
	//   │    ├── file2-1
	//   │    └── file2-2
	//   └── dir3/
	//        ├── file3-1
	//        └── file3-2

	dir1 := path.Join(rootDirectory, "dir1")
	dir2 := path.Join(rootDirectory, "dir2") // this will be skipped
	dir3 := path.Join(rootDirectory, "dir3")

	// Create map of all files to create with full paths
	fileStructure := map[string][]string{
		dir1: {
			path.Join(dir1, "file1-1"),
			path.Join(dir1, "file1-2"),
		},
		dir2: { // dir2 will be skipped during walk
			path.Join(dir2, "file2-1"),
			path.Join(dir2, "file2-2"),
		},
		dir3: {
			path.Join(dir3, "file3-1"),
			path.Join(dir3, "file3-2"),
		},
	}

	// Create all files in all directories
	for _, files := range fileStructure {
		for _, filePath := range files {
			err := s.StorageDriver.PutContent(s.ctx, filePath, s.blobberFactory.GetBlobber(32).GetAllBytes())
			require.NoError(s.T(), err)
		}
	}

	// Build expected and unexpected paths
	expectedPaths := []string{
		dir1,
		dir3,
		fileStructure[dir1][0],
		fileStructure[dir1][1],
		fileStructure[dir3][0],
		fileStructure[dir3][1],
	}

	// Unexpected paths are dir2 and all its files
	unexpectedPaths := []string{
		dir2,
		fileStructure[dir2][0],
		fileStructure[dir2][1],
	}

	// Helper function to create a walk function that skips dir2
	createWalkFunc := func(pathCollector *sync.Map) storagedriver.WalkFn {
		return func(fInfo storagedriver.FileInfo) error {
			path := fInfo.Path()

			// Skip dir2
			if path == dir2 {
				return storagedriver.ErrSkipDir
			}

			// Record this path was visited
			pathCollector.Store(path, struct{}{})
			return nil
		}
	}

	// Verify all expected paths were visited and unexpected were not
	verifyPaths := func(visitedPaths *sync.Map) {
		// Check expected paths were visited
		for _, expectedPath := range expectedPaths {
			_, found := visitedPaths.Load(expectedPath)
			assert.Truef(s.T(), found, "Path %s should have been visited", expectedPath)
		}

		// Check unexpected paths were not visited
		for _, unexpectedPath := range unexpectedPaths {
			_, found := visitedPaths.Load(unexpectedPath)
			assert.Falsef(s.T(), found, "Path %s should not have been visited", unexpectedPath)
		}
	}

	s.Run("PlainWalk", func() {
		var visitedPaths sync.Map
		err := s.StorageDriver.Walk(s.ctx, rootDirectory, createWalkFunc(&visitedPaths))
		require.NoError(s.T(), err)
		verifyPaths(&visitedPaths)
	})

	s.Run("ParallelWalk", func() {
		s.skipIfWalkParallelIsNotSupported()

		var visitedPaths sync.Map
		err := s.StorageDriver.WalkParallel(s.ctx, rootDirectory, createWalkFunc(&visitedPaths))
		require.NoError(s.T(), err)
		verifyPaths(&visitedPaths)
	})
}