From ef6d8fda9b061141447ab6b39d744586a53406d0 Mon Sep 17 00:00:00 2001 From: ccx1024cc <1261138729@qq.com> Date: Tue, 3 Jan 2017 10:33:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0metrics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 - .metrics | 114 +++++++++++++++++++ Gemfile | 5 + coverage/rcov/rcov.txt | 0 db/schema.rb | 2 +- spec/controllers/sessions_controller_spec.rb | 8 +- spec/controllers/users_controller_spec.rb | 4 +- spec/models/user_spec.rb | 19 ---- 8 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 .metrics create mode 100644 coverage/rcov/rcov.txt diff --git a/.gitignore b/.gitignore index bf9cabf..0641825 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,6 @@ # or operating system, you probably want to add a global ignore instead: # git config --global core.excludesfile '~/.gitignore_global' -# Ignore bundler config. -/.bundle - # Ignore the default SQLite database. /db/*.sqlite3 /db/*.sqlite3-journal diff --git a/.metrics b/.metrics new file mode 100644 index 0000000..9c7cbef --- /dev/null +++ b/.metrics @@ -0,0 +1,114 @@ +# by default, your directory base name +# MetricFu.report_name = 'Something Convenient' + +# cane +# Fails your build if code quality thresholds are not met +# dirs_to_cane: MetricFu::Io::FileSystem.directory("code_dirs"), +# options: +# abc_max: 15, +# line_length: 80, +# no_doc: "n", +# no_readme: "n", +# filetypes: ["rb"] +MetricFu.configuration.configure_metric(:cane) do |cane| + cane.enabled = false +end + +# churn +# Identifies the file changes too frequently +# options : +# minimum_churn_count +# ignore_files +# start_date +# data_directory +# history +# report +# name +MetricFu.configuration.configure_metric(:churn) do |churn| + churn.enabled = true + churn.start_date = '6 months ago' +end + +# flog +# measuring code complexity +# options : +# dirs_to_flog +# continue +# all +# quiet +MetricFu.configuration.configure_metric(:flog) do |flog| + flog.enabled = false + flog.dirs_to_flog = ['app'] +end + +# flay +# Flay analyzes code for structural similarities. Differences in literal +# values, variable, class, method names, whitespace, programming style, +# braces vs do/end, etc are all ignored. Making this totally rad. +MetricFu.configuration.configure_metric(:flay) do |flay| + flay.enabled = true + flay.dirs_to_flay = ['app', 'script'] + # MetricFu has been setting the minimum score as 100 for + # a long time. This is a really big number, considering + # the default is 16. Setting it to nil to use the Flay default. + minimum_score = nil +end + +# reek +# find code smell +MetricFu.configuration.configure_metric(:reek) do |reek| + reek.enabled = true + reek.dirs_to_reek = ['app'] + dirs_to_reek = nil +end + +# roodi +# find a large number of general issues, design issue +# options +# dirs_to_roodi +# roodi_config +MetricFu.configuration.configure_metric(:roodi) do |roodi| + roodi.enabled = true + roodi.dirs_to_roodi = ['app'] +end + +# SAIKURO +# Saikuro is a Ruby cyclomatic complexity analyzer +# optins : +# cyclo: "", +# filter_cyclo: "0", +# warn_cyclo: "5", +# error_cyclo: "7", +# formater: "text", +MetricFu.configuration.configure_metric(:saikuro) do |saikuro| + saikuro.enabled = true + saikuro.output_directory = 'tmp/metric_fu/scratch/saikuro' + saikuro.input_directory = ['app'] +end + +# code statics +# options +# additional_test_directories +# additional_app_directories +MetricFu.configuration.configure_metric(:stats) do |stats| + stats.enabled = true +end + +# rails best practices +MetricFu::Configuration.run do |config| + config.configure_metric(:rails_best_practices) do |rbp| + # rbp.silent = true + # rbp.exclude = ["config/chef"] + end +end + +# simplecover +# calculate coverage +MetricFu::Configuration.run do |config| + config.configure_metric(:rcov) do |rcov| + rcov.coverage_file = MetricFu.run_path.join("coverage/rcov/rcov.txt") + rcov.rcov_opts = ["--profile", "--rails", "--exclude /gems/,/Library/,spec"] + rcov.enable + rcov.activate + end +end diff --git a/Gemfile b/Gemfile index 547b9ef..7e799e0 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,11 @@ group :development, :test do gem 'rspec-rails', '~> 3.5.2' #把 Rails 生成测试数据默认使用的固件换成更好用的预构件 gem 'factory_girl_rails','~> 4.7.0' + + # 安装分析工具metrics + gem 'simplecov' + gem 'simplecov-rcov-text' + gem 'metric_fu' end group :development do diff --git a/coverage/rcov/rcov.txt b/coverage/rcov/rcov.txt new file mode 100644 index 0000000..e69de29 diff --git a/db/schema.rb b/db/schema.rb index 42d373a..9d0a985 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -44,8 +44,8 @@ ActiveRecord::Schema.define(version: 20161229084919) do t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "category" t.integer "project_id" + t.integer "category" end add_index "notes", ["user_id"], name: "index_notes_on_user_id" diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 35e0ac0..849c282 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -26,9 +26,9 @@ RSpec.describe SessionsController, type: :controller do expect(controller.session[:user_id]).to eq @user1.id end - it 'should get ok and text: login success' do + it 'should get ok and text: ok' do expect(response).to have_http_status :ok - expect(response.body).to eq 'login success' + expect(response.body).to eq 'ok' end end @@ -43,8 +43,8 @@ RSpec.describe SessionsController, type: :controller do expect(controller.session[:user_id].nil?).to be true end - it 'return with 200 and text: account or password is not correct' do - expect(response).to have_http_status :ok + it 'return with 401' do + expect(response).to have_http_status :unauthorized expect(response.body).to eq 'account or password is not correct' end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index c9cd0f9..8315bbf 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -9,13 +9,13 @@ RSpec.describe UsersController, type: :controller do describe 'GET #emailExist' do it 'valid email' do get :emailExist, email: user.email - expect(response.body).to eq 'not exist' + expect(response.body).to eq '0' end it 'duplicated email' do user = create(:user_with_sequence_number) get :emailExist, email: user.email - expect(response.body).to eq 'exist' + expect(response.body).to eq '1' end it 'should not throw exception with no email param' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 86656bc..3848467 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -11,13 +11,6 @@ RSpec.describe User, type: :model do expect(build(:user, name: nil)).not_to be_valid end - it 'does not allow duplicate username' do - create(:user_with_sequence_number, name: 'ccx') - user_duplicated_name = build(:user_with_sequence_number, name: 'ccx') - user_duplicated_name.valid? - expect(user_duplicated_name.errors[:name].size).to eq(1) - end - it 'does not allow name length > 30' do expect(build(:user_name_length_gt_30)).not_to be_valid end @@ -28,13 +21,6 @@ RSpec.describe User, type: :model do expect(build(:user, email: nil)).not_to be_valid end - it 'does not allow duplicate email' do - create(:user_with_sequence_number, email: '1261138729@qq.com') - user_duplicated_email = build(:user_with_sequence_number, email: '1261138729@qq.com') - user_duplicated_email.valid? - expect(user_duplicated_email.errors[:email].size).to eq(1) - end - it 'does not allow email length > 50' do expect(build(:user_email_length_gt_50)).not_to be_valid end @@ -87,11 +73,6 @@ RSpec.describe User, type: :model do expect(build(:user, phone: '1371234567')).to_not be_valid end - it 'does not allow duplicated phone' do - create(:user) - expect(build(:user)).to_not be_valid - end - # 防js注入 it 'does not allow multiline' do expect(build(:user, phone: '13712345\n78')).to_not be_valid