Repository: apple/swift-nio
File: Tests/NIOFileSystemIntegrationTests/FileSystemTests.swift (Swift)

Code before:

        guard
            let info = try await self.fs.info(
                forFileAt: temporaryDirectoryPath,
                infoAboutSymbolicLink: false
            )
        else {
            XCTFail("The temporary directory could not be accessed.")
            return
        }
        XCTAssertEqual(info.type, .directory)
        XCTAssertGreaterThan(info.size, 0)
    }

    func testTemporaryDirectoryRespectsEnvironmentVariable() async throws {
        let originalTMPDIR = getenv("TMPDIR")
        defer {
            if let originalTMPDIR {
                setenv("TMPDIR", String(cString: originalTMPDIR), 1)
            } else {
                unsetenv("TMPDIR")
            }
        }

        let customTmpDir = "/custom/tmp/dir"
        setenv("TMPDIR", customTmpDir, 1)
        let tmpDir = try await fs.temporaryDirectory
        XCTAssertEqual(tmpDir.string, customTmpDir)
    }

    func testReadChunksRange() async throws {
        try await self.fs.withFileHandle(forReadingAt: FilePath(#filePath)) { handle in
            let info = try await handle.info()
            let size = info.size
            let endIndex = size + 1

            let ranges: [(Range<Int64>, Int)] = [
                (0..<0, 0),
                (0..<1, 1),
                (0..<endIndex, Int(size)),
                (1..<endIndex, Int(size - 1)),
                (0..<endIndex + 1, Int(size)),
                (1..<endIndex + 1, Int(size - 1)),
                (0..<Int64.max, Int(size)),
            ]

            for (range, expected) in ranges {
                let byteCount = try await handle.readChunks(in: range).reduce(into: 0) {
                    $0 += $1.readableBytes
                }


Review comment (suggestion):
I'm a bit hesitant to mess around with `TMPDIR` like this in case other tests running in parallel are relying on it. 

II think my preference here would be to check if `TMPDIR` is set and then check `fs.temporaryDirectory` if it was set.

Code after:

        guard
            let info = try await self.fs.info(
                forFileAt: temporaryDirectoryPath,
                infoAboutSymbolicLink: false
            )
        else {
            XCTFail("The temporary directory could not be accessed.")
            return
        }
        XCTAssertEqual(info.type, .directory)
        XCTAssertGreaterThan(info.size, 0)
    }

    func testTemporaryDirectoryRespectsEnvironment() async throws {
        if let envTmpDir = getenv("TMPDIR") {
            let envTmpDirString = String(cString: envTmpDir)
            let fsTempDirectory = try await fs.temporaryDirectory
            XCTAssertEqual(fsTempDirectory, FilePath(envTmpDirString))
        }
    }

    func testReadChunksRange() async throws {
        try await self.fs.withFileHandle(forReadingAt: FilePath(#filePath)) { handle in
            let info = try await handle.info()
            let size = info.size
            let endIndex = size + 1

            let ranges: [(Range<Int64>, Int)] = [
                (0..<0, 0),
                (0..<1, 1),
                (0..<endIndex, Int(size)),
                (1..<endIndex, Int(size - 1)),
                (0..<endIndex + 1, Int(size)),
                (1..<endIndex + 1, Int(size - 1)),
                (0..<Int64.max, Int(size)),
            ]

            for (range, expected) in ranges {
                let byteCount = try await handle.readChunks(in: range).reduce(into: 0) {
                    $0 += $1.readableBytes
                }

                XCTAssertEqual(byteCount, expected)
            }
        }
    }

    func testReadChunksClosedRange() async throws {
        try await self.fs.withFileHandle(forReadingAt: FilePath(#filePath)) { handle in
            let info = try await handle.info()