-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Skip quitting applications when not logged into GUI. #5010
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "utils/user" | ||
|
||
describe User do | ||
subject { described_class.current } | ||
|
||
it { is_expected.to eq ENV["USER"] } | ||
|
||
describe "#gui?" do | ||
before do | ||
allow(SystemCommand).to receive(:run).with("who") | ||
.and_return([who_output, "", instance_double(Process::Status, success?: true)]) | ||
end | ||
|
||
context "when the current user is in a console session" do | ||
let(:who_output) { | ||
<<~EOS | ||
#{ENV["USER"]} console Oct 1 11:23 | ||
#{ENV["USER"]} ttys001 Oct 1 11:25 | ||
EOS | ||
} | ||
|
||
its(:gui?) { is_expected.to be true } | ||
end | ||
|
||
context "when the current user is not in a console session" do | ||
let(:who_output) { | ||
<<~EOS | ||
#{ENV["USER"]} ttys001 Oct 1 11:25 | ||
fake_user ttys002 Oct 1 11:27 | ||
EOS | ||
} | ||
|
||
its(:gui?) { is_expected.to be false } | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "delegate" | ||
require "etc" | ||
|
||
require "system_command" | ||
|
||
class User < DelegateClass(String) | ||
def gui? | ||
out, _, status = system_command "who" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checks whether the user has a logged in GUI session. It does not check whether the current session is part of that GUI session. For example, if you are logged in via the GUI and the Homebrew session is via SSH, this will return true, but AppleScript commands will still fail. Have you tested any cases where this will return false? What are those cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @reitermarkus let me know what you think of the above! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, it should check the current session, not any session. I'm not sure how to do that, however. Feel free to send a PR to improve this. |
||
return false unless status.success? | ||
out.lines | ||
.map(&:split) | ||
.any? { |user, type,| user == self && type == "console" } | ||
end | ||
|
||
def self.current | ||
@current ||= new(Etc.getpwuid(Process.euid).name) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the move?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason it cannot be called inside a
DelegateClass
, so it needs to be defined insideKernel
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird!