Skip to content

Commit

Permalink
Improve System::User specs on Windows (#15156)
Browse files Browse the repository at this point in the history
* Ensures `USER_NAME` and `USER_ID` are correct if the current username contains whitespace characters.
* Normalizes the username portion of the user ID in assertions as well, since it seems some versions of Windows would break the specs without it. We already do this to the domain portion.
  • Loading branch information
HertzDevil authored Nov 6, 2024
1 parent 0cac615 commit a92a6c2
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions spec/std/system/user_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "spec"
require "system/user"

{% if flag?(:win32) %}
{% name, id = `whoami /USER /FO TABLE /NH`.stringify.chomp.split(" ") %}
USER_NAME = {{ name }}
USER_ID = {{ id }}
{% parts = `whoami /USER /FO TABLE /NH`.stringify.chomp.split(" ") %}
USER_NAME = {{ parts[0..-2].join(" ") }}
USER_ID = {{ parts[-1] }}
{% else %}
USER_NAME = {{ `id -un`.stringify.chomp }}
USER_ID = {{ `id -u`.stringify.chomp }}
Expand All @@ -17,8 +17,7 @@ def normalized_username(username)
# on Windows, domain names are case-insensitive, so we unify the letter case
# from sources like `whoami`, `hostname`, or Win32 APIs
{% if flag?(:win32) %}
domain, _, user = username.partition('\\')
"#{domain.upcase}\\#{user}"
username.upcase
{% else %}
username
{% end %}
Expand Down

0 comments on commit a92a6c2

Please sign in to comment.