Skip to content

Commit b628c68

Browse files
committed
Disable testPlatformOptionsRunAsUser on CI if we don't have privilege to create a temporary user
1 parent b5a5ec6 commit b628c68

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

Tests/SubprocessTests/SubprocessTests+Windows.swift

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ extension SubprocessWindowsTests {
458458
extension SubprocessWindowsTests {
459459
@Test(.enabled(if: SubprocessWindowsTests.hasAdminPrivileges()))
460460
func testPlatformOptionsRunAsUser() async throws {
461-
try await self.withTemporaryUser { username, password in
461+
try await self.withTemporaryUser { username, password, succeed in
462462
// Use public directory as working directory so the newly created user
463463
// has access to it
464464
let workingDirectory = FilePath("C:\\Users\\Public")
@@ -501,6 +501,12 @@ extension SubprocessWindowsTests {
501501
}
502502
return String(decodingCString: pointer, as: UTF16.self)
503503
}
504+
// On CI, we might failed to create user due to privilege issues
505+
// There's nothing much we can do in this case
506+
guard succeed else {
507+
// If we fail to create the user, skip this test
508+
return true
509+
}
504510
// CreateProcessWithLogonW doesn't appear to work when running in a container
505511
return whoamiResult.terminationStatus == .unhandledException(STATUS_DLL_INIT_FAILED) && userName() == "ContainerAdministrator"
506512
}
@@ -735,16 +741,16 @@ extension SubprocessWindowsTests {
735741
// MARK: - User Utils
736742
extension SubprocessWindowsTests {
737743
private func withTemporaryUser(
738-
_ work: (String, String) async throws -> Void
744+
_ work: (String, String, Bool) async throws -> Void
739745
) async throws {
740746
let username: String = "TestUser\(randomString(length: 5, lettersOnly: true))"
741747
let password: String = "Password\(randomString(length: 10))"
742748

743-
func createUser(withUsername username: String, password: String) {
744-
username.withCString(
749+
func createUser(withUsername username: String, password: String) -> Bool {
750+
return username.withCString(
745751
encodedAs: UTF16.self
746752
) { usernameW in
747-
password.withCString(
753+
return password.withCString(
748754
encodedAs: UTF16.self
749755
) { passwordW in
750756
var userInfo: USER_INFO_1 = USER_INFO_1()
@@ -765,27 +771,30 @@ extension SubprocessWindowsTests {
765771
&error
766772
)
767773
guard status == NERR_Success else {
768-
Issue.record("Failed to create user with error: \(error)")
769-
return
774+
return false
770775
}
776+
return true
771777
}
772778
}
773779
}
774780

775-
createUser(withUsername: username, password: password)
781+
let succeed = createUser(withUsername: username, password: password)
782+
776783
defer {
777-
// Now delete the user
778-
let status = username.withCString(
779-
encodedAs: UTF16.self
780-
) { usernameW in
781-
return NetUserDel(nil, usernameW)
782-
}
783-
if status != NERR_Success {
784-
Issue.record("Failed to delete user with error: \(status)")
784+
if succeed {
785+
// Now delete the user
786+
let status = username.withCString(
787+
encodedAs: UTF16.self
788+
) { usernameW in
789+
return NetUserDel(nil, usernameW)
790+
}
791+
if status != NERR_Success {
792+
Issue.record("Failed to delete user with error: \(status)")
793+
}
785794
}
786795
}
787796
// Run work
788-
try await work(username, password)
797+
try await work(username, password, succeed)
789798
}
790799

791800
private static func hasAdminPrivileges() -> Bool {

0 commit comments

Comments
 (0)