Skip to content

Commit e3dea81

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

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
@@ -455,7 +455,7 @@ extension SubprocessWindowsTests {
455455
extension SubprocessWindowsTests {
456456
@Test(.enabled(if: SubprocessWindowsTests.hasAdminPrivileges()))
457457
func testPlatformOptionsRunAsUser() async throws {
458-
try await self.withTemporaryUser { username, password in
458+
try await self.withTemporaryUser { username, password, succeed in
459459
// Use public directory as working directory so the newly created user
460460
// has access to it
461461
let workingDirectory = FilePath("C:\\Users\\Public")
@@ -498,6 +498,12 @@ extension SubprocessWindowsTests {
498498
}
499499
return String(decodingCString: pointer, as: UTF16.self)
500500
}
501+
// On CI, we might failed to create user due to privilege issues
502+
// There's nothing much we can do in this case
503+
guard succeed else {
504+
// If we fail to create the user, skip this test
505+
return true
506+
}
501507
// CreateProcessWithLogonW doesn't appear to work when running in a container
502508
return whoamiResult.terminationStatus == .unhandledException(STATUS_DLL_INIT_FAILED) && userName() == "ContainerAdministrator"
503509
}
@@ -693,16 +699,16 @@ extension SubprocessWindowsTests {
693699
// MARK: - User Utils
694700
extension SubprocessWindowsTests {
695701
private func withTemporaryUser(
696-
_ work: (String, String) async throws -> Void
702+
_ work: (String, String, Bool) async throws -> Void
697703
) async throws {
698704
let username: String = "TestUser\(randomString(length: 5, lettersOnly: true))"
699705
let password: String = "Password\(randomString(length: 10))"
700706

701-
func createUser(withUsername username: String, password: String) {
702-
username.withCString(
707+
func createUser(withUsername username: String, password: String) -> Bool {
708+
return username.withCString(
703709
encodedAs: UTF16.self
704710
) { usernameW in
705-
password.withCString(
711+
return password.withCString(
706712
encodedAs: UTF16.self
707713
) { passwordW in
708714
var userInfo: USER_INFO_1 = USER_INFO_1()
@@ -723,27 +729,30 @@ extension SubprocessWindowsTests {
723729
&error
724730
)
725731
guard status == NERR_Success else {
726-
Issue.record("Failed to create user with error: \(error)")
727-
return
732+
return false
728733
}
734+
return true
729735
}
730736
}
731737
}
732738

733-
createUser(withUsername: username, password: password)
739+
let succeed = createUser(withUsername: username, password: password)
740+
734741
defer {
735-
// Now delete the user
736-
let status = username.withCString(
737-
encodedAs: UTF16.self
738-
) { usernameW in
739-
return NetUserDel(nil, usernameW)
740-
}
741-
if status != NERR_Success {
742-
Issue.record("Failed to delete user with error: \(status)")
742+
if succeed {
743+
// Now delete the user
744+
let status = username.withCString(
745+
encodedAs: UTF16.self
746+
) { usernameW in
747+
return NetUserDel(nil, usernameW)
748+
}
749+
if status != NERR_Success {
750+
Issue.record("Failed to delete user with error: \(status)")
751+
}
743752
}
744753
}
745754
// Run work
746-
try await work(username, password)
755+
try await work(username, password, succeed)
747756
}
748757

749758
private static func hasAdminPrivileges() -> Bool {

0 commit comments

Comments
 (0)