Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
annotate
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
annotate
Commits
52757575
Commit
52757575
authored
Oct 03, 2017
by
Hideki IGARASHI
Committed by
Cuong Tran
Oct 02, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix problem with specifying model files as argument (#514)
* Refactor AnnotateModels#get_model_files * Add the tests for `AnnotateModels#get_model_files` * Remove unnecessary `reject` call
parent
7c8e916d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
146 additions
and
12 deletions
+146
-12
annotate_models.rb
lib/annotate/annotate_models.rb
+30
-12
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+116
-0
No files found.
lib/annotate/annotate_models.rb
View file @
52757575
...
@@ -649,34 +649,52 @@ module AnnotateModels
...
@@ -649,34 +649,52 @@ module AnnotateModels
# of model files from root dir. Otherwise we take all the model files
# of model files from root dir. Otherwise we take all the model files
# in the model_dir directory.
# in the model_dir directory.
def
get_model_files
(
options
)
def
get_model_files
(
options
)
models
=
[]
model_files
=
[]
unless
options
[
:is_rake
]
models
=
ARGV
.
dup
.
reject
{
|
m
|
m
.
match
(
/^(.*)=/
)
}
model_files
=
list_model_files_from_argument
unless
options
[
:is_rake
]
end
return
model_files
unless
model_files
.
empty?
if
models
.
empty?
begin
model_dir
.
each
do
|
dir
|
model_dir
.
each
do
|
dir
|
Dir
.
chdir
(
dir
)
do
Dir
.
chdir
(
dir
)
do
lst
=
list
=
if
options
[
:ignore_model_sub_dir
]
if
options
[
:ignore_model_sub_dir
]
Dir
[
"*.rb"
].
map
{
|
f
|
[
dir
,
f
]
}
Dir
[
"*.rb"
].
map
{
|
f
|
[
dir
,
f
]
}
else
else
Dir
[
"**/*.rb"
].
reject
{
|
f
|
f
[
"concerns/"
]
}.
map
{
|
f
|
[
dir
,
f
]
}
Dir
[
"**/*.rb"
].
reject
{
|
f
|
f
[
"concerns/"
]
}.
map
{
|
f
|
[
dir
,
f
]
}
end
end
models
.
concat
(
l
st
)
model_files
.
concat
(
li
st
)
end
end
end
end
model_files
rescue
SystemCallError
rescue
SystemCallError
puts
"No models found in directory '
#{
model_dir
.
join
(
"', '"
)
}
'."
puts
"No models found in directory '
#{
model_dir
.
join
(
"', '"
)
}
'."
puts
"Either specify models on the command line, or use the --model-dir option."
puts
"Either specify models on the command line, or use the --model-dir option."
puts
"Call 'annotate --help' for more info."
puts
"Call 'annotate --help' for more info."
exit
1
exit
1
end
end
def
list_model_files_from_argument
return
[]
if
ARGV
.
empty?
specified_files
=
ARGV
.
map
{
|
file
|
File
.
expand_path
(
file
)
}
model_files
=
model_dir
.
flat_map
do
|
dir
|
absolute_dir_path
=
File
.
expand_path
(
dir
)
specified_files
.
find_all
{
|
file
|
file
.
start_with?
(
absolute_dir_path
)
}
.
map
{
|
file
|
[
dir
,
file
.
sub
(
"
#{
absolute_dir_path
}
/"
,
''
)]
}
end
if
model_files
.
size
!=
specified_files
.
size
puts
"The specified file could not be found in directory '
#{
model_dir
.
join
(
"', '"
)
}
'."
puts
"Call 'annotate --help' for more info."
exit
1
end
end
models
model
_file
s
end
end
private
:list_model_files_from_argument
# Retrieve the classes belonging to the model names we're asked to process
# Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models
# Check for namespaced models in subdirectories as well as models
...
...
spec/annotate/annotate_models_spec.rb
View file @
52757575
...
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
...
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
require
'annotate/annotate_models'
require
'annotate/annotate_models'
require
'annotate/active_record_patch'
require
'annotate/active_record_patch'
require
'active_support/core_ext/string'
require
'active_support/core_ext/string'
require
'files'
describe
AnnotateModels
do
describe
AnnotateModels
do
def
mock_index
(
name
,
params
=
{})
def
mock_index
(
name
,
params
=
{})
...
@@ -951,6 +952,121 @@ EOS
...
@@ -951,6 +952,121 @@ EOS
end
end
end
end
describe
'#get_model_files'
do
subject
{
described_class
.
get_model_files
(
options
)
}
before
do
ARGV
.
clear
described_class
.
model_dir
=
[
model_dir
]
end
context
'when `model_dir` is valid'
do
let
(
:model_dir
)
do
Files
do
file
'foo.rb'
dir
'bar'
do
file
'baz.rb'
dir
'qux'
do
file
'quux.rb'
end
end
dir
'concerns'
do
file
'corge.rb'
end
end
end
context
'when the model files are not specified'
do
context
'when no option is specified'
do
let
(
:options
)
{
{}
}
it
'returns all model files under `model_dir` directory'
do
is_expected
.
to
contain_exactly
(
[
model_dir
,
'foo.rb'
],
[
model_dir
,
File
.
join
(
'bar'
,
'baz.rb'
)],
[
model_dir
,
File
.
join
(
'bar'
,
'qux'
,
'quux.rb'
)]
)
end
end
context
'when `ignore_model_sub_dir` option is enabled'
do
let
(
:options
)
{
{
ignore_model_sub_dir:
true
}
}
it
'returns model files just below `model_dir` directory'
do
is_expected
.
to
contain_exactly
([
model_dir
,
'foo.rb'
])
end
end
end
context
'when the model files are specified'
do
let
(
:additional_model_dir
)
{
'additional_model'
}
let
(
:model_files
)
do
[
File
.
join
(
model_dir
,
'foo.rb'
),
"./
#{
File
.
join
(
additional_model_dir
,
'corge/grault.rb'
)
}
"
# Specification by relative path
]
end
before
{
ARGV
.
concat
(
model_files
)
}
context
'when no option is specified'
do
let
(
:options
)
{
{}
}
context
'when all the specified files are in `model_dir` directory'
do
before
do
described_class
.
model_dir
<<
additional_model_dir
end
it
'returns specified files'
do
is_expected
.
to
contain_exactly
(
[
model_dir
,
'foo.rb'
],
[
additional_model_dir
,
'corge/grault.rb'
]
)
end
end
context
'when a model file outside `model_dir` directory is specified'
do
it
'exits with the status code'
do
begin
subject
raise
rescue
SystemExit
=>
e
expect
(
e
.
status
).
to
eq
(
1
)
end
end
end
end
context
'when `is_rake` option is enabled'
do
let
(
:options
)
{
{
is_rake:
true
}
}
it
'returns all model files under `model_dir` directory'
do
is_expected
.
to
contain_exactly
(
[
model_dir
,
'foo.rb'
],
[
model_dir
,
File
.
join
(
'bar'
,
'baz.rb'
)],
[
model_dir
,
File
.
join
(
'bar'
,
'qux'
,
'quux.rb'
)]
)
end
end
end
end
context
'when `model_dir` is invalid'
do
let
(
:model_dir
)
{
'/not_exist_path'
}
let
(
:options
)
{
{}
}
it
'exits with the status code'
do
begin
subject
raise
rescue
SystemExit
=>
e
expect
(
e
.
status
).
to
eq
(
1
)
end
end
end
end
describe
'#get_model_class'
do
describe
'#get_model_class'
do
require
'tmpdir'
require
'tmpdir'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment