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

Code before:
        try await self.fs.withDirectoryHandle(atPath: linkPath) { dir in
            let info = try await dir.info()
            XCTAssertEqual(info.type, .directory)
            XCTAssertGreaterThan(info.size, 0)
        }
    }

    func testCurrentWorkingDirectory() async throws {
        let directory = try await self.fs.currentWorkingDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
    }

    func testTemporaryDirectory() async throws {
        let directory = try await self.fs.temporaryDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
    }

    func testHomeDirectory() async throws {
        let directory = try await self.fs.homeDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
        let info = try await self.fs.info(forFileAt: directory, infoAboutSymbolicLink: false)
        XCTAssertEqual(info?.type, .directory)
    }

    func testInfo() async throws {
        let info = try await self.fs.info(forFileAt: .testDataReadme, infoAboutSymbolicLink: false)
        XCTAssertEqual(info?.type, .regular)
        XCTAssertGreaterThan(info?.size ?? -1, Int64(0))
    }

    func testInfoResolvingSymbolicLinks() async throws {
        let info = try await self.fs.info(
            forFileAt: .testDataReadmeSymlink,
            infoAboutSymbolicLink: false
        )
        XCTAssertEqual(info?.type, .regular)
        XCTAssertGreaterThan(info?.size ?? -1, Int64(0))
    }

    func testInfoWithoutResolvingSymbolicLinks() async throws {
        let info = try await self.fs.info(
            forFileAt: .testDataReadmeSymlink,
            infoAboutSymbolicLink: true
        )
        XCTAssertEqual(info?.type, .symlink)
        XCTAssertGreaterThan(info?.size ?? -1, Int64(0))
    }


Review comment (suggestion):
In an ideal world we'd have tests for the environment variable behaviours distinctly from the fallback to the password database. You cannot safely modify the existing process environment (`setenv` is not thread-safe), and we can't use Swift Testing's exit tests because they don't provide a way for us to modify the environment of the subprocess.

Can I suggest that we should have `@_spi(Testing)` entry points for each of the sub pieces, and write tests that exercise them? That way at least we can confirm they behave as expected.

Code after:
        try await self.fs.withDirectoryHandle(atPath: linkPath) { dir in
            let info = try await dir.info()
            XCTAssertEqual(info.type, .directory)
            XCTAssertGreaterThan(info.size, 0)
        }
    }

    func testCurrentWorkingDirectory() async throws {
        let directory = try await self.fs.currentWorkingDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
    }

    func testTemporaryDirectory() async throws {
        let directory = try await self.fs.temporaryDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
    }

    func testHomeDirectory() async throws {
        let directory = try await self.fs.homeDirectory
        XCTAssert(!directory.underlying.isEmpty)
        XCTAssert(directory.underlying.isAbsolute)
        let info = try await self.fs.info(forFileAt: directory, infoAboutSymbolicLink: false)
        XCTAssertEqual(info?.type, .directory)
    }

    func testHomeDirectoryFromEnvironment() async throws {
        // Should return a value when HOME is set (which it typically is)
        if let path = Libc.homeDirectoryFromEnvironment() {
            XCTAssert(!path.isEmpty)
            XCTAssert(path.isAbsolute)

            // Verify it matches the high-level API
            let fsHome = try await self.fs.homeDirectory
            XCTAssertEqual(path, fsHome.underlying)
        } else {
            // If it returns nil, then HOME check should fail
            let home = getenv("HOME")
            XCTAssertTrue(home == nil || home!.pointee == 0, "Expected HOME to be unset or empty")

            #if os(Windows)
            let profile = getenv("USERPROFILE")
            XCTAssertTrue(profile == nil || profile!.pointee == 0, "Expected USERPROFILE to be unset or empty")
            #endif
        }
    }

    #if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
    func testHomeDirectoryFromPasswd() {
        // Should always succeed on Unix-like systems for the current user