Skip to content

Tests: Convert Basics/FileSystem/* to Swift Testing #8448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Sources/_InternalTestSupport/Process.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

public import Foundation

public enum OperatingSystem: Hashable, Sendable {
case macOS
case windows
case linux
case android
case unknown
}

extension ProcessInfo {
#if os(macOS)
public static let hostOperatingSystem = OperatingSystem.macOS
#elseif os(Linux)
public static let hostOperatingSystem = OperatingSystem.linux
#elseif os(Windows)
public static let hostOperatingSystem = OperatingSystem.windows
#else
public static let hostOperatingSystem = OperatingSystem.unknown
#endif

#if os(Windows)
public static let EOL = "\r\n"
#else
public static let EOL = "\n"
#endif

#if os(Windows)
public static let exeSuffix = ".exe"
#else
public static let exeSuffix = ""
#endif
}
70 changes: 70 additions & 0 deletions Sources/_InternalTestSupport/SkippedTestSupport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import class Foundation.FileManager
import class Foundation.ProcessInfo
import Testing

extension Trait where Self == Testing.ConditionTrait {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given Testing.ConditionTrait conforms to Trait, can't we simply write this instead?

extension Testing.ConditionTrait {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was a direct copy from IntegartionTests/Sources/IntegrationTests. I want to avoid changing it drastically until #8223 is completed

/// Skip test if the host operating system does not match the running OS.
public static func requireHostOS(_ os: OperatingSystem, when condition: Bool = true) -> Self {
enabled("This test requires a \(os) host OS.") {
ProcessInfo.hostOperatingSystem == os && condition
}
}

/// Skip test if the host operating system matches the running OS.
public static func skipHostOS(_ os: OperatingSystem, _ comment: Comment? = nil) -> Self {
disabled(comment ?? "This test cannot run on a \(os) host OS.") {
ProcessInfo.hostOperatingSystem == os
}
}

/// Skip test unconditionally
public static func skip(_ comment: Comment? = nil) -> Self {
disabled(comment ?? "Unconditional skip, a comment should be added for the reason") { true }
}

/// Skip test if the environment is self hosted.
public static func skipSwiftCISelfHosted(_ comment: Comment? = nil) -> Self {
disabled(comment ?? "SwiftCI is self hosted") {
ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil
}
}

/// Skip test if the test environment has a restricted network access, i.e. cannot get to internet.
public static func requireUnrestrictedNetworkAccess(_ comment: Comment? = nil) -> Self {
disabled(comment ?? "CI Environment has restricted network access") {
ProcessInfo.processInfo.environment["SWIFTCI_RESTRICTED_NETWORK_ACCESS"] != nil
}
}

/// Skip test if built by XCode.
public static func skipIfXcodeBuilt() -> Self {
disabled("Tests built by Xcode") {
#if Xcode
true
#else
false
#endif
}
}

/// Constructs a condition trait that causes a test to be disabled if the Foundation process spawning implementation
/// is not using `posix_spawn_file_actions_addchdir`.
public static var requireThreadSafeWorkingDirectory: Self {
disabled("Thread-safe process working directory support is unavailable.") {
// Amazon Linux 2 has glibc 2.26, and glibc 2.29 is needed for posix_spawn_file_actions_addchdir_np support
FileManager.default.contents(atPath: "/etc/system-release")
.map { String(decoding: $0, as: UTF8.self) == "Amazon Linux release 2 (Karoo)\n" } ?? false
}
}
}
36 changes: 19 additions & 17 deletions Tests/BasicsTests/FileSystem/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@testable import Basics
import TSCTestSupport
import XCTest
import Testing

final class FileSystemTests: XCTestCase {
func testStripFirstLevelComponent() throws {
struct FileSystemTests {
@Test
func stripFirstLevelComponent() throws {
let fileSystem = InMemoryFileSystem()

let rootPath = AbsolutePath("/root")
Expand All @@ -35,29 +35,31 @@ final class FileSystemTests: XCTestCase {

do {
let contents = try fileSystem.getDirectoryContents(.root)
XCTAssertEqual(contents.count, 1)
#expect(contents.count == 1)
}

try fileSystem.stripFirstLevel(of: .root)

do {
let contents = Set(try fileSystem.getDirectoryContents(.root))
XCTAssertEqual(contents.count, totalDirectories + totalFiles)
#expect(contents.count == totalDirectories + totalFiles)

for index in 0 ..< totalDirectories {
XCTAssertTrue(contents.contains("dir\(index)"))
#expect(contents.contains("dir\(index)"))
}
for index in 0 ..< totalFiles {
XCTAssertTrue(contents.contains("file\(index)"))
#expect(contents.contains("file\(index)"))
}
}
}

func testStripFirstLevelComponentErrors() throws {
@Test
func stripFirstLevelComponentErrors() throws {
let functionUnderTest = "stripFirstLevel"
do {
let fileSystem = InMemoryFileSystem()
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
#expect(throws: StringError("\(functionUnderTest) requires single top level directory")) {
try fileSystem.stripFirstLevel(of: .root)
}
}

Expand All @@ -67,8 +69,8 @@ final class FileSystemTests: XCTestCase {
let path = AbsolutePath.root.appending("dir\(index)")
try fileSystem.createDirectory(path, recursive: false)
}
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
#expect(throws: StringError("\(functionUnderTest) requires single top level directory")) {
try fileSystem.stripFirstLevel(of: .root)
}
}

Expand All @@ -78,17 +80,17 @@ final class FileSystemTests: XCTestCase {
let path = AbsolutePath.root.appending("file\(index)")
try fileSystem.writeFileContents(path, string: "\(index)")
}
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
#expect(throws: StringError("\(functionUnderTest) requires single top level directory")) {
try fileSystem.stripFirstLevel(of: .root)
}
}

do {
let fileSystem = InMemoryFileSystem()
let path = AbsolutePath.root.appending("file")
try fileSystem.writeFileContents(path, string: "")
XCTAssertThrowsError(try fileSystem.stripFirstLevel(of: .root), "expected error") { error in
XCTAssertMatch((error as? StringError)?.description, .contains("requires single top level directory"))
#expect(throws: StringError("\(functionUnderTest) requires single top level directory")) {
try fileSystem.stripFirstLevel(of: .root)
}
}
}
Expand Down
45 changes: 23 additions & 22 deletions Tests/BasicsTests/FileSystem/PathShimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,65 @@

import Basics
import Foundation
import XCTest
import Testing

class PathShimTests: XCTestCase {
func testRescursiveDirectoryCreation() {
// For the tests we'll need a temporary directory.
struct PathShimTests {
@Test
func rescursiveDirectoryCreation() {
try! withTemporaryDirectory(removeTreeOnDeinit: true) { path in
// Create a directory under several ancestor directories.
let dirPath = path.appending(components: "abc", "def", "ghi", "mno", "pqr")
try! makeDirectories(dirPath)

// Check that we were able to actually create the directory.
XCTAssertTrue(localFileSystem.isDirectory(dirPath))
#expect(localFileSystem.isDirectory(dirPath))

// Check that there's no error if we try to create the directory again.
try! makeDirectories(dirPath)
#expect(throws: Never.self) {
try! makeDirectories(dirPath)
}
}
}
}

class WalkTests: XCTestCase {
func testNonRecursive() throws {
#if os(Android)
struct WalkTests {
@Test
func nonRecursive() throws {
#if os(Android)
let root = "/system"
var expected: [AbsolutePath] = [
"\(root)/usr",
"\(root)/bin",
"\(root)/etc",
]
#elseif os(Windows)
let expectedCount = 3
#elseif os(Windows)
let root = ProcessInfo.processInfo.environment["SystemRoot"]!
var expected: [AbsolutePath] = [
"\(root)/System32",
"\(root)/SysWOW64",
]
#else
let expectedCount = (root as NSString).pathComponents.count + 2
#else
let root = ""
var expected: [AbsolutePath] = [
"/usr",
"/bin",
"/sbin",
]
#endif
let expectedCount = 2
#endif
for x in try walk(AbsolutePath(validating: "\(root)/"), recursively: false) {
if let i = expected.firstIndex(of: x) {
expected.remove(at: i)
}
#if os(Android)
XCTAssertEqual(3, x.components.count)
#elseif os(Windows)
XCTAssertEqual((root as NSString).pathComponents.count + 2, x.components.count)
#else
XCTAssertEqual(2, x.components.count)
#endif
#expect(x.components.count == expectedCount, "Actual is not as expected")
}
XCTAssertEqual(expected.count, 0)
#expect(expected.count == 0)
}

func testRecursive() {
@Test
func recursive() {
let root = AbsolutePath(#file).parentDirectory.parentDirectory.parentDirectory.parentDirectory
.appending(component: "Sources")
var expected = [
Expand All @@ -82,6 +83,6 @@ class WalkTests: XCTestCase {
expected.remove(at: i)
}
}
XCTAssertEqual(expected, [])
#expect(expected == [])
}
}
Loading