1
1
module Elastic
2
2
class Reporter
3
- JSON_FILES = "#{ File . expand_path ( './tmp/rest-api-spec/api' ) } /*.json" . freeze
3
+ STACK_FILES = "#{ File . expand_path ( './tmp/rest-api-spec/api' ) } /*.json" . freeze
4
4
TESTS_PATH = File . expand_path ( '../tests/**/*.yml' )
5
5
6
6
attr_reader :apis , :tested , :untested
7
7
8
8
def initialize
9
- @apis = gather_apis
9
+ @apis = { }
10
+ @apis [ :specification ] = specification_apis
11
+ @apis [ :json ] = json_apis
10
12
@tested = [ ]
11
13
@untested = [ ]
12
14
report!
13
15
end
14
16
15
- def gather_apis
16
- endpoints = Dir [ JSON_FILES ] . map do |path |
17
- path . split ( '/' ) . last . gsub ( '.json' , '' )
18
- end . reject { |a | a . split ( '/' ) . last . gsub ( '.json' , '' ) . start_with? ( '_' ) }
17
+ # Stack APIs are obtained from the Elasticsearch Rest JSON specification.
18
+ # Use `rake download_stack` to download the spec files to ../tmp.
19
+ #
20
+ def json_apis
21
+ apis = Dir [ STACK_FILES ] . map { |path | path . split ( '/' ) . last . gsub ( '.json' , '' ) }
22
+ reject_internal ( apis )
23
+ end
24
+
25
+ # Serverless APIs are obtained from elastic/elasticsearch-specification.
26
+ # Use `rake download_serverless` to download the files to ../tmp.
27
+ def specification_apis
28
+ apis = JSON . parse ( File . read ( './tmp/schema.json' ) ) [ 'endpoints' ] . map do |s |
29
+ { 'name' => s [ 'name' ] , 'availability' => s [ 'availability' ] }
30
+ end
31
+ reject_internal ( apis )
32
+ end
33
+
34
+ def serverless_apis
35
+ # The absence of an 'availability' field on a property implies that the property is
36
+ # available in all flavors.
37
+ @apis [ :specification ] . select do |api |
38
+ api . dig ( 'availability' ) . nil? ||
39
+ (
40
+ !!api . dig ( 'availability' , 'serverless' ) &&
41
+ api . dig ( 'availability' , 'serverless' , 'visibility' ) == 'public'
42
+ )
43
+ end
19
44
end
20
45
21
46
def report!
22
- @apis . each do |api |
47
+ @apis [ :json ] . each do |api |
23
48
if ( test = find_test ( api ) )
24
49
@tested << test
25
50
else
@@ -28,12 +53,32 @@ def report!
28
53
end
29
54
end
30
55
56
+ def coverage
57
+ percentage = @tested . count * 100 / @apis [ :json ] . count
58
+ ""
59
+ end
60
+
61
+ private
62
+
31
63
def find_test ( endpoint )
32
64
Dir [ TESTS_PATH ] . map do |path |
33
65
relative_path = path [ path . index ( '/tests' ) ..-1 ]
34
- return { endpoint : endpoint , file : ".#{ relative_path } " } if File . readlines ( path ) . grep ( /#{ endpoint } / ) . any?
66
+
67
+ if File . readlines ( path ) . grep ( /#{ endpoint } / ) . any?
68
+ return { endpoint : endpoint , file : ".#{ relative_path } " }
69
+ end
35
70
end
36
71
false
37
72
end
73
+
74
+ def reject_internal ( apis )
75
+ apis . reject! do |api |
76
+ if api . is_a? ( Hash )
77
+ api . dig ( 'name' ) . start_with? ( '_' )
78
+ elsif api . is_a? ( String )
79
+ api . start_with? ( '_' )
80
+ end
81
+ end
82
+ end
38
83
end
39
84
end
0 commit comments