Skip to content
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

Don't check update if stdout is non-tty #43

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/one_gadget/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
module OneGadget
# For automatically check update.
module Update
# At least 7 days between check for new version.
FREQUENCY = 7 * 24 * 60 * 60
# At least 30 days between check for new version.
FREQUENCY = 30 * 24 * 60 * 60
# Path to cache file.
CACHE_FILE = File.join(ENV['HOME'], '.cache', 'one_gadget', 'update').freeze

Expand All @@ -27,16 +27,17 @@ def check!
end

# show update message
msg = format("A newer version of OneGadget is available (%s --> %s).\n", OneGadget::VERSION, latest)
msg << "Update with: $ gem update one_gadget\n\n"
OneGadget::Logger.info(msg)
msg = format('A newer version of OneGadget is available (%s --> %s).', OneGadget::VERSION, latest)
OneGadget::Helper.ask_update(msg: msg)
end

private

# check ~/.cache/one_gadget/update
def need_check?
cache = cache_file
# don't check if not CLI
return false unless $stdout.tty?
return false if cache.nil? # cache file fails, no update check.
return false if IO.binread(cache).strip == 'never'
Time.now >= last_check + FREQUENCY
Expand Down
5 changes: 3 additions & 2 deletions spec/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
@hook_cache_file.call do |path|
expect(described_class.__send__(:need_check?)).to be false
now = Time.now
allow(Time).to receive(:now).and_return(now + 7 * 24 * 3600)
allow(Time).to receive(:now).and_return(now + 30 * 24 * 3600)
allow($stdout).to receive(:tty?).and_return(true)
expect(described_class.__send__(:need_check?)).to be true
IO.binwrite(path, 'never')
expect(described_class.__send__(:need_check?)).to be false
Expand All @@ -53,7 +54,7 @@
[OneGadget] You have the latest version of OneGadget
EOS
stub_const('OneGadget::VERSION', '0.0.0')
expect { hook_logger { described_class.check! } }.to output(include(<<-EOS)).to_stdout
expect { hook_logger { described_class.check! } }.to output(include(<<-EOS.strip)).to_stdout
$ gem update one_gadget
EOS
end
Expand Down