Skip to content

Commit 086c8d8

Browse files
committed
Disable testPlatformOptionsRunAsUser on CI if we don't have privilege to create a temporary user
1 parent 199eb72 commit 086c8d8

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
@@ -452,7 +452,7 @@ extension SubprocessWindowsTests {
452452
extension SubprocessWindowsTests {
453453
@Test(.enabled(if: SubprocessWindowsTests.hasAdminPrivileges()))
454454
func testPlatformOptionsRunAsUser() async throws {
455-
try await self.withTemporaryUser { username, password in
455+
try await self.withTemporaryUser { username, password, succeed in
456456
// Use public directory as working directory so the newly created user
457457
// has access to it
458458
let workingDirectory = FilePath("C:\\Users\\Public")
@@ -495,6 +495,12 @@ extension SubprocessWindowsTests {
495495
}
496496
return String(decodingCString: pointer, as: UTF16.self)
497497
}
498+
// On CI, we might failed to create user due to privilege issues
499+
// There's nothing much we can do in this case
500+
guard succeed else {
501+
// If we fail to create the user, skip this test
502+
return true
503+
}
498504
// CreateProcessWithLogonW doesn't appear to work when running in a container
499505
return whoamiResult.terminationStatus == .unhandledException(STATUS_DLL_INIT_FAILED) && userName() == "ContainerAdministrator"
500506
}
@@ -729,16 +735,16 @@ extension SubprocessWindowsTests {
729735
// MARK: - User Utils
730736
extension SubprocessWindowsTests {
731737
private func withTemporaryUser(
732-
_ work: (String, String) async throws -> Void
738+
_ work: (String, String, Bool) async throws -> Void
733739
) async throws {
734740
let username: String = "TestUser\(randomString(length: 5, lettersOnly: true))"
735741
let password: String = "Password\(randomString(length: 10))"
736742

737-
func createUser(withUsername username: String, password: String) {
738-
username.withCString(
743+
func createUser(withUsername username: String, password: String) -> Bool {
744+
return username.withCString(
739745
encodedAs: UTF16.self
740746
) { usernameW in
741-
password.withCString(
747+
return password.withCString(
742748
encodedAs: UTF16.self
743749
) { passwordW in
744750
var userInfo: USER_INFO_1 = USER_INFO_1()
@@ -759,27 +765,30 @@ extension SubprocessWindowsTests {
759765
&error
760766
)
761767
guard status == NERR_Success else {
762-
Issue.record("Failed to create user with error: \(error)")
763-
return
768+
return false
764769
}
770+
return true
765771
}
766772
}
767773
}
768774

769-
createUser(withUsername: username, password: password)
775+
let succeed = createUser(withUsername: username, password: password)
776+
770777
defer {
771-
// Now delete the user
772-
let status = username.withCString(
773-
encodedAs: UTF16.self
774-
) { usernameW in
775-
return NetUserDel(nil, usernameW)
776-
}
777-
if status != NERR_Success {
778-
Issue.record("Failed to delete user with error: \(status)")
778+
if succeed {
779+
// Now delete the user
780+
let status = username.withCString(
781+
encodedAs: UTF16.self
782+
) { usernameW in
783+
return NetUserDel(nil, usernameW)
784+
}
785+
if status != NERR_Success {
786+
Issue.record("Failed to delete user with error: \(status)")
787+
}
779788
}
780789
}
781790
// Run work
782-
try await work(username, password)
791+
try await work(username, password, succeed)
783792
}
784793

785794
private static func hasAdminPrivileges() -> Bool {

0 commit comments

Comments
 (0)