Skip to content

Commit

Permalink
Improved update checking (#43)
Browse files Browse the repository at this point in the history
* Don't check update if stdout is non-tty
* Change update checking frequency from 7 days to 30 days
  • Loading branch information
david942j authored Sep 13, 2018
1 parent bc35ada commit 667586f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
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 @@ -28,16 +28,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'

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

0 comments on commit 667586f

Please sign in to comment.