From f47ee2e154eb8c32434c1fc49de94e30b1e9d16f Mon Sep 17 00:00:00 2001 From: uu59 Date: Wed, 14 May 2014 18:00:13 +0900 Subject: [PATCH] Fix spec on CI, and refactor --- app/models/plugin.rb | 35 ++++++++++++++++++----------------- spec/models/plugin_spec.rb | 21 +++++++++++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/models/plugin.rb b/app/models/plugin.rb index 8ccc2f4..951bc38 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -16,7 +16,7 @@ class Plugin def install! if valid? && !installed? if fluent_gem("install", gem_name, "-v", version) - File.open(gemfile, "a") do |f| + File.open(gemfile_path, "a") do |f| f.puts format_gemfile end end @@ -27,11 +27,11 @@ class Plugin if valid? && installed? # NOTE: do not uninstall gem actually for now. because it is not necessary, and slow job new_gemfile = "" - File.open(gemfile).each_line do |line| + File.open(gemfile_path).each_line do |line| next if line.strip == format_gemfile new_gemfile << line end - File.open(gemfile, "w"){|f| f.write new_gemfile } + File.open(gemfile_path, "w"){|f| f.write new_gemfile } end end @@ -46,7 +46,7 @@ class Plugin end def installed? - File.read(gemfile).lines.map(&:strip).grep(format_gemfile).present? + File.read(gemfile_path).lines.map(&:strip).grep(format_gemfile).present? end def format_gemfile @@ -54,35 +54,36 @@ class Plugin end def self.gemfile_changed? - # if true, rails server needs to restart - @initial_gemfile_content != File.read(gemfile) + # if true, rails server needs to restart } + @initial_gemfile_content != File.read(gemfile_path) end - def self.gemfile + def self.gemfile_path if Rails.env == "test" - gemfile = Rails.root + "tmp/Gemfile.plugins" + gemfile_path = "/tmp/fluentd-ui-test-Gemfile.plugins" # can't create a file under Rails.root directory on Circle CI else - gemfile = Rails.root + "Gemfile.plugins" + gemfile_path = Rails.root + "Gemfile.plugins" end + end - unless File.exists?(gemfile) - File.open(gemfile, "w") do |f| + def self.pristine! + unless File.exists?(gemfile_path) + File.open(gemfile_path, "w") do |f| f.write "# USED BY fluentd-ui internally\n" end end - @initial_gemfile_content ||= File.read(gemfile) - gemfile + @initial_gemfile_content = File.read(gemfile_path) end + pristine! - - def gemfile - self.class.gemfile + def gemfile_path + self.class.gemfile_path end private def fluent_gem(*commands) - unless system(*%W(bundle exec gem) + commands) # TODO: should grab stdout/stderr + unless system(*%W(bundle exec fluent-gem) + commands) # TODO: should grab stdout/stderr raise GemError, "failed command #{commands}" end true diff --git a/spec/models/plugin_spec.rb b/spec/models/plugin_spec.rb index f9a972e..02e492a 100644 --- a/spec/models/plugin_spec.rb +++ b/spec/models/plugin_spec.rb @@ -2,15 +2,24 @@ require 'spec_helper' describe Plugin do let(:plugin) { FactoryGirl.build(:plugin) } + before do + Kernel.stub(:system) # do not call `system('fluent-gem install ..')` on CI + end + after do + File.unlink Plugin.gemfile_path if File.exist?(Plugin.gemfile_path) + Plugin.pristine! + end describe "#valid?" do describe "gem_name" do subject { plugin } before { plugin.gem_name = gem_name } + context "nil is invalid" do let(:gem_name) { nil } it { should_not be_valid } end + context "somthing filled is valid" do let(:gem_name) { "foobar" } it { should be_valid } @@ -20,10 +29,12 @@ describe Plugin do describe "version" do subject { plugin } before { plugin.version = version } + context "nil is invalid" do let(:version) { nil } it { should_not be_valid } end + context "somthing filled is valid" do let(:version) { "0.0.1" } it { should be_valid } @@ -32,11 +43,6 @@ describe Plugin do end describe "#install!" do - before do - File.unlink Plugin.gemfile - Plugin.instance_variable_set(:@initial_gemfile_content, nil) - end - describe "invoke fluent_gem" do after do plugin.stub(:valid?).and_return { valid } @@ -85,13 +91,12 @@ describe Plugin do end describe "uninstall!" do - let(:installed_plugin) { FactoryGirl.build(:plugin) } + let(:installed_plugin) { FactoryGirl.build(:plugin, gem_name: "fluent-plugin-foobar") } before do installed_plugin.stub(:fluent_gem).and_return { true } - File.unlink Plugin.gemfile installed_plugin.install! - Plugin.instance_variable_set(:@initial_gemfile_content, nil) + Plugin.pristine! end before do