swagcov 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5aaf6a7a919c70669dc24a129159b2ce961aa237495d57cfd1d94350bbad526f
4
- data.tar.gz: e312bf832ceca1bc66017b131c958ab9f70ea4efddc32fac9bda0fcb369d7696
3
+ metadata.gz: 8aec9444a5e8dfd1d4b23fc61c710d4ccc28d5c7985df0fbef5e8c02e16e7519
4
+ data.tar.gz: a19a3c8f73ad9efb814ff7469b6306bacb52d296893130b7fb42740d1d7e7db4
5
5
  SHA512:
6
- metadata.gz: 14de18f74737e418b16153aea8c9f107545b9445fc5956dd0617ddbae4a0ad78be83017d6df8f442724186d38fd44a47b8462d4dc7ce2590c2b7aae26aaff201
7
- data.tar.gz: b22dc442442935535df66b494d9f24f4475517eb36468208d24a3ad3b43f49202eb6ada1679efc1657804ea2e789b62b8dac030009ad1bf282fc9b6b3cfa1853
6
+ metadata.gz: cc64199a92c0322fe3c42bbaa882d103a9a8f18e1c3dbe02ddb5b56bb21d13eae763536fbcdb2f84aa8a045fb6d92726f1a953eaa27595118cbe6a7324dd5c2a
7
+ data.tar.gz: 9713fa38210e7f6273e2f9004a7dc791994ebd80e50e0852381de005f67b1c238ce995060c3643b2b1db7520d46687defa554067e3ff2a75ae71ec70a809f496
data/README.md CHANGED
@@ -26,7 +26,7 @@ Create a `.swagcov.yml` in root of your Rails application.
26
26
  ```yml
27
27
  docs:
28
28
  paths:
29
- - swagger/v1/swagger.yaml
29
+ - swagger.yaml
30
30
  ```
31
31
 
32
32
  - Add `only` routes (**optional**) :
@@ -49,7 +49,7 @@ Create a `.swagcov.yml` in root of your Rails application.
49
49
  ```yml
50
50
  docs:
51
51
  paths:
52
- - swagger/v1/swagger.yaml
52
+ - swagger.yaml
53
53
 
54
54
  routes:
55
55
  paths:
@@ -91,7 +91,7 @@ bundle exec rake swagcov
91
91
  ```yml
92
92
  docs:
93
93
  paths:
94
- - swagger/v1/swagger.yaml
94
+ - swagger.yaml
95
95
 
96
96
  routes:
97
97
  paths:
@@ -136,7 +136,9 @@ bundle exec rspec spec --exclude-pattern spec/sandbox_**/**/*_spec.rb
136
136
  - Ensure you have access to publish on [rubygems](https://rubygems.org/gems/swagcov).
137
137
  - Update [CHANGELOG](https://github.com/smridge/swagcov/blob/main/CHANGELOG.md).
138
138
  - Update [`VERSION`](https://github.com/smridge/swagcov/blob/main/lib/swagcov/version.rb).
139
- - Commit changes to `main` branch locally.
139
+ - Run `bundle update` for each sandbox application to reflect new swagcov version in each `Gemfile.lock`
140
+ - Open a Pull Request to ensure all specs pass, then merge to `main`.
141
+ - Checkout the latest `main` on your machine.
140
142
  - Run: `rake release`
141
143
  - This command builds the gem, creates a tag and publishes to rubygems, see [bundler docs](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
142
144
 
@@ -31,8 +31,10 @@ module Swagcov
31
31
  attr_reader :dotfile
32
32
 
33
33
  def collect_coverage
34
+ openapi_files = ::Swagcov::OpenapiFiles.new(filepaths: dotfile.doc_paths)
35
+
34
36
  @routes.each do |route|
35
- path = route.path.spec.to_s.sub(/\(\.:format\)$/, "")
37
+ path = route.path.spec.to_s.chomp("(.:format)")
36
38
 
37
39
  next if third_party_route?(route, path)
38
40
 
@@ -45,30 +47,16 @@ module Swagcov
45
47
  next if dotfile.only_path_mismatch?(path)
46
48
 
47
49
  @total += 1
48
- regex = Regexp.new("^#{path.gsub(%r{:[^/]+}, '\\{[^/]+\\}')}(\\.[^/]+)?$")
49
- matching_keys = docs_paths.keys.grep(regex)
50
50
 
51
- if (doc = docs_paths.dig(matching_keys.first, route.verb.downcase))
51
+ if (response_keys = openapi_files.find_response_keys(path: path, route_verb: route.verb))
52
52
  @covered += 1
53
- @routes_covered << { verb: route.verb, path: path, status: doc["responses"].keys.map(&:to_s).sort.join(" ") }
53
+ @routes_covered << { verb: route.verb, path: path, status: response_keys.join(" ") }
54
54
  else
55
55
  @routes_not_covered << { verb: route.verb, path: path, status: "none" }
56
56
  end
57
57
  end
58
58
  end
59
59
 
60
- def docs_paths
61
- @docs_paths ||= Dir.glob(dotfile.doc_paths).reduce({}) do |acc, docspath|
62
- acc.merge(load_yaml(docspath))
63
- end
64
- end
65
-
66
- def load_yaml docspath
67
- YAML.load_file(docspath)["paths"]
68
- rescue Psych::SyntaxError
69
- raise BadConfigurationError, "Malinformed openapi file (#{docspath})"
70
- end
71
-
72
60
  def third_party_route? route, path
73
61
  # https://github.com/rails/rails/blob/48f3c3e201b57a4832314b2c957a3b303e89bfea/actionpack/lib/action_dispatch/routing/inspector.rb#L105-L107
74
62
  # Skips route paths like ["/rails/info/properties", "/rails/info", "/rails/mailers"]
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swagcov
4
+ class OpenapiFiles
5
+ def initialize filepaths:
6
+ @filepaths = filepaths
7
+ @openapi_paths = load_yamls
8
+ end
9
+
10
+ def find_response_keys path:, route_verb:
11
+ # replace :id with {id}
12
+ regex = Regexp.new("^#{path.gsub(%r{:[^/]+}, '\\{[^/]+\\}')}?$")
13
+
14
+ matching_paths_key = @openapi_paths.keys.grep(regex).first
15
+ matching_request_method_key = @openapi_paths.dig(matching_paths_key, route_verb.downcase)
16
+
17
+ matching_request_method_key["responses"].keys.map(&:to_s).sort if matching_request_method_key
18
+ end
19
+
20
+ private
21
+
22
+ def load_yamls
23
+ Dir.glob(@filepaths).reduce({}) do |hash, filepath|
24
+ hash.merge(load_yaml(filepath))
25
+ end
26
+ end
27
+
28
+ def load_yaml filepath
29
+ YAML.load_file(filepath)["paths"]
30
+ rescue Psych::SyntaxError
31
+ raise BadConfigurationError, "Malinformed openapi file (#{filepath})"
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Swagcov
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/swagcov.rb CHANGED
@@ -6,6 +6,7 @@ if defined?(Rails)
6
6
  require "swagcov/railtie"
7
7
  require "swagcov/dotfile"
8
8
  require "swagcov/coverage"
9
+ require "swagcov/openapi_files"
9
10
  end
10
11
 
11
12
  require "swagcov/core_ext/string"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagcov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sarah Ridge
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-21 00:00:00.000000000 Z
11
+ date: 2022-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,7 @@ files:
108
108
  - lib/swagcov/core_ext/string.rb
109
109
  - lib/swagcov/coverage.rb
110
110
  - lib/swagcov/dotfile.rb
111
+ - lib/swagcov/openapi_files.rb
111
112
  - lib/swagcov/railtie.rb
112
113
  - lib/swagcov/version.rb
113
114
  - lib/tasks/swagcove.rake
@@ -135,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
136
  - !ruby/object:Gem::Version
136
137
  version: '0'
137
138
  requirements: []
138
- rubygems_version: 3.2.32
139
+ rubygems_version: 3.2.33
139
140
  signing_key:
140
141
  specification_version: 4
141
142
  summary: Open API docs coverage for Rails Routes