-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRakefile
146 lines (121 loc) · 4.08 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'bundler/gem_tasks'
task default: :spec
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = ['--color', '--format doc']
end
require 'rubocop/rake_task'
desc 'Run RuboCop on the lib directory'
RuboCop::RakeTask.new(:rubocop) do |task|
task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
# don't abort rake on failure
task.fail_on_error = false
end
require 'json'
require 'yaml'
require 'fileutils'
desc 'Parse OpenSPF tests'
def spec_file_name(doc, output_path)
description = doc['description']
file_prefix = description.gsub(/[^\w\s_-]+/, '')
.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
.gsub(/\s+/, '_')
file_name = "#{file_prefix}_spec.rb"
File.join(output_path, file_name)
end
INDENT_STRING = ' '.freeze
def indented_string(num_indents = 1)
Array.new(num_indents) { INDENT_STRING }.join
end
def puts_prefixed_string(f, s, indent = 0)
f.write indented_string(indent) unless indent.zero?
f.puts s
end
def empty_line(f)
puts_prefixed_string(f, '', 0)
end
def write_zonedata(f, zonedata, indent = 1)
puts_prefixed_string(f, 'let(:zonefile) do', indent)
puts_prefixed_string(f, zonedata, indent + 1)
puts_prefixed_string(f, 'end', indent)
empty_line(f)
dns_line = 'let(:dns_client) { DNSAdapter::MockClient.new(zonefile) }'
puts_prefixed_string(f, dns_line, indent)
puts_prefixed_string(f, 'let(:options) { { dns_client: dns_client } }',
indent)
empty_line(f)
end
def escape_quote(val)
return unless val
val.gsub("'") { |_s| "\\'" }
end
def clean_description(description)
return unless description
description.tr("\n", ' ').delete("'")
end
def as_array(val)
return [] unless val
val.is_a?(Array) ? val : [val]
end
def write_comment(f, comment, indent)
return unless comment
comment.lines do |comment_line|
puts_prefixed_string(f, "# #{comment_line}", indent)
end
end
def write_result(f, host, mailfrom, helo, indent)
spf_result =
"Coppertone::SpfService.authenticate_email('#{host}', '#{mailfrom}', " \
"'#{helo}', options)"
puts_prefixed_string(f, "result = #{spf_result}", indent)
end
def write_expects(f, results, explanation, indent)
results_array = "[#{results.map { |r| ":#{r}" }.join(',')}]"
code_expect = "expect(#{results_array}).to include(result.code)"
puts_prefixed_string(f, code_expect, indent)
return unless explanation
exp_expect = "expect(result.explanation).to eq('#{explanation}')"
puts_prefixed_string(f, exp_expect, indent)
end
def normalize_spec_args(spec)
helo = escape_quote(spec['helo'])
host = escape_quote(spec['host'])
mailfrom = escape_quote(spec['mailfrom'])
comment = escape_quote(spec['comment'])
description = clean_description(spec['description'])
results = as_array(spec['result'])
explanation = escape_quote(spec['explanation'])
[helo, host, mailfrom, comment, description, results, explanation]
end
def write_spec(f, spec, indent = 1)
helo, host, mailfrom, comment, description, results, explanation =
normalize_spec_args(spec)
puts_prefixed_string(f, "it '#{description}' do", indent)
write_comment(f, comment, indent + 1)
write_result(f, host, mailfrom, helo, indent + 1)
write_expects(f, results, explanation, indent + 1)
puts_prefixed_string(f, 'end', indent)
empty_line(f)
end
def write_doc(doc, output_path)
File.open(spec_file_name(doc, output_path), 'w') do |f|
puts_prefixed_string(f, "require 'spec_helper'")
empty_line(f)
description = doc['description']
puts_prefixed_string(f, "describe '#{description}' do")
write_zonedata(f, doc['zonedata'], 1)
doc['tests'].each do |k, spec|
spec['description'] ||= k
write_spec(f, spec)
end
puts_prefixed_string(f, 'end')
end
end
task :build_open_spf_test_suite do
yml_file_name = 'rfc7208-tests.yml'
yml_file_path = File.join(File.dirname(__FILE__), 'spec', yml_file_name)
output_path = File.join(File.dirname(__FILE__), 'spec', 'open_spf')
FileUtils.mkdir_p(output_path)
documents = YAML.load_stream(File.read(yml_file_path))
documents.each { |doc| write_doc(doc, output_path) }
end