-
Notifications
You must be signed in to change notification settings - Fork 403
/
Rakefile
132 lines (105 loc) · 3.11 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
# frozen_string_literal: true
require 'bundler'
Bundler::GemHelper.install_tasks
require 'rubocop/rake_task'
RuboCop::RakeTask.new
task default: [:rubocop, 'test:ruby']
require 'rake/testtask'
namespace :test do
desc %(Run all tests)
task all: [:rubocop, :lint_javascript, 'test:ruby', 'test:js']
desc %(Test Ruby code)
Rake::TestTask.new(:ruby) do |test|
test.libs << 'lib' << 'test'
test.test_files = Dir.glob("#{__dir__}/test/**/test_*.rb")
test.warning = false
end
desc %(Test JavaScript code)
task js: ['regenerate_javascript', 'test:server', 'test:qunit']
desc %(Starts the test server)
task :server do
puts "Opening test app at #{test_url} ..."
server_command = "bundle exec rackup test/javascript/config.ru -q -p #{test_port}"
if ENV['UI']
system server_command
else
@server = fork { exec server_command }
end
# Give Sinatra some time to start
sleep 3
end
desc %(Starts the test server which reloads everything on each refresh)
task :reloadable do
exec "bundle exec shotgun test/javascript/config.ru -p #{test_port} --server webrick"
end
desc %(Starts qunit tests)
task :qunit do
if ENV['UI']
system(*browse_cmd(url))
else
run_headless_tests
end
end
end
desc %(Lint JavaScript files)
task :lint_javascript do
run_pnpm_script 'eslint'
end
desc %(Regenerate JavaScript files)
task :regenerate_javascript do
run_pnpm_script 'build'
end
desc %(Commit JavaScript files)
task :commit_javascript do
perform_git_commit
end
def perform_git_commit
system('git add dist vendor', out: File::NULL, err: File::NULL)
if system('git commit -m "Regenerated JavaScript files"', out: File::NULL, err: File::NULL)
puts 'Committed changes'
else
puts 'Nothing to commit'
end
end
# Returns an array e.g.: ['open', 'http://example.com']
# rubocop:disable Metrics/CyclomaticComplexity
def browse_cmd(url)
require 'rbconfig'
browser = ENV.fetch('BROWSER', nil) ||
(RbConfig::CONFIG['host_os'].include?('darwin') && 'open') ||
(RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/ && 'start') ||
%w[xdg-open x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
abort('ERROR: no web browser detected') unless browser
Array(browser) << url
end
# rubocop:enable Metrics/CyclomaticComplexity
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = "#{path}/#{cmd}#{ext}"
return exe if File.executable? exe
end
end
nil
end
def run_headless_tests
run_pnpm_script 'test', "#{test_url}?autostart=false" do
Process.kill 'INT', @server
end
end
def run_pnpm_script(script, options = '')
require 'English'
system "pnpm #{script} #{options}"
exit_code = $CHILD_STATUS.exitstatus
yield if block_given?
exit exit_code unless exit_code.zero?
end
def test_port
@test_port ||= 4567
end
def test_url
@test_url ||= "http://localhost:#{test_port}"
end
task build: %i[regenerate_javascript commit_javascript]