Skip to content

Tests: Enable BasicsTests/FileSystem/* Tests on Windows (Swift Testing) #8450

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

Merged
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
58 changes: 33 additions & 25 deletions Tests/BasicsTests/FileSystem/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,94 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import TSCTestSupport
import Testing

@testable import Basics
import TSCTestSupport
import XCTest

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

let rootPath = AbsolutePath("/root")
try fileSystem.createDirectory(rootPath)

let totalDirectories = Int.random(in: 0 ..< 100)
for index in 0 ..< totalDirectories {
let totalDirectories = Int.random(in: 0..<100)
for index in 0..<totalDirectories {
let path = rootPath.appending("dir\(index)")
try fileSystem.createDirectory(path, recursive: false)
}

let totalFiles = Int.random(in: 0 ..< 100)
for index in 0 ..< totalFiles {
let totalFiles = Int.random(in: 0..<100)
for index in 0..<totalFiles {
let path = rootPath.appending("file\(index)")
try fileSystem.writeFileContents(path, string: "\(index)")
}

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)"))
for index in 0..<totalDirectories {
#expect(contents.contains("dir\(index)"))
}
for index in 0 ..< totalFiles {
XCTAssertTrue(contents.contains("file\(index)"))
for index in 0..<totalFiles {
#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)
}
}

do {
let fileSystem = InMemoryFileSystem()
for index in 0 ..< 3 {
for index in 0..<3 {
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)
}
}

do {
let fileSystem = InMemoryFileSystem()
for index in 0 ..< 3 {
for index in 0..<3 {
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
Loading