diff --git a/lib/one_gadget/update.rb b/lib/one_gadget/update.rb index a55ef20e..1dc750cf 100644 --- a/lib/one_gadget/update.rb +++ b/lib/one_gadget/update.rb @@ -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 @@ -28,9 +28,8 @@ 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 @@ -38,6 +37,8 @@ def check! # 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' diff --git a/spec/update_spec.rb b/spec/update_spec.rb index b9bc4158..0f0b67c3 100644 --- a/spec/update_spec.rb +++ b/spec/update_spec.rb @@ -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 @@ -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