This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
test.rake
123 lines (101 loc) · 4.32 KB
/
test.rake
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
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
# frozen_string_literal: true
namespace :test do
# Run html-proofer to check for broken links
desc 'Build site, check for broken links, write report to a file'
task links: %w[build links_no_build]
desc 'Check the existing _site for broken EXTERNAL links'
task :external_links do
puts 'Testing external links'
system 'bin/htmlproofer _site/ --external_only'
end
desc 'Check the existing _site for broken INTERNAL links'
task :html do
# puts 'Checking HTML ...'.magenta
# LinkChecker.check_site
puts 'Link checking has been disabled since the project is no longer supported'.magenta
end
desc 'Check the existing _site for broken links and report to a separate file'
task :links_no_build do
# Write console output (stderr only) to a file.
# Use this if you need to also capture stdout: https://stackoverflow.com/a/2480439
report = LinkChecker.md_report_path
$stderr.reopen(report, 'w+')
Rake::Task['test:html'].invoke
# We're expecting link validation errors, but unless we rescue from
# StandardError, rake will abort and won't run the convert task (https://stackoverflow.com/a/10048406).
# Wrapping task in a begin-rescue block prevent rake from aborting.
# Seems to prevent printing an error count though.
rescue StandardError => e
# Show how many lines contains the Markdown report
puts e.to_s.red
puts "To see the report, open the #{report} file.".red
end
desc 'Report about broken links in HTML'
task report: %w[links] do
puts 'Converting the link check report to HTML...'.magenta
Converter.to_html
end
desc 'Test Markdown style with mdl'
task :md do
# puts 'Testing Markdown style with mdl ...'.magenta
# print 'List the rules: $ '.magenta
# sh 'bin/mdl -l --style=_checks/styles/style-rules-prod'
# puts 'Linting ...'.magenta
# output =
# `bin/mdl \
# --style=_checks/styles/style-rules-prod \
# --ignore-front-matter \
# --git-recurse \
# -- .`
# puts output.yellow
# abort 'Fix the reported issues'.red unless output.empty?
# puts 'No issues found'.green
puts 'Markdown testing has been disabled since the project is no longer supported'.magenta
end
desc 'Find unused images. To exclude by regex pattern, use the "exclude_img" argument. Example of excluding a "layout" directory: "rake test:unused_includes exclude_img=/layout/"'
task :unused_images do
puts 'Running a task for finding unused images (png,svg,jpeg,jpg,ico)'.magenta
images = FileList['src/**/*.{png,svg,jpeg,jpg,ico}']
puts "The project contains a total of #{images.size} images."
exclude = ENV['exclude_img']
images.exclude(/#{exclude}/) if exclude
puts 'Checking for unlinked images...'
Dir['src/**/*.{md,html,js,css}'].each do |file|
# Exclude symmlinks
next if File.symlink? file
images.delete_if { |image| File.read(file).include?(File.basename(image)) }
end
if images.empty?
puts 'No unlinked images'.green
else
images.each do |image|
puts "No links for #{image}".yellow
end
puts "Found #{images.size} dangling images".red
end
end
desc 'Find unused includes. To exclude by regex pattern, use the "exclude_incl" argument. Example of excluding a "layout" directory: "rake test:unused_includes exclude_incl=/layout/"'
task :unused_includes do
puts 'Running a task to find unused _includes'.magenta
includes = FileList['src/_includes/**/*']
puts "The project contains a total of #{includes.size} includes"
exclude = ENV['exclude_incl']
includes.exclude(/#{exclude}/) if exclude
Dir['src/**/*.{md,html}'].each do |file|
next if File.symlink? file
includes.delete_if { |include| File.read(file).include?(File.basename(include)) }
end
if includes.empty?
puts 'No unlinked includes'.green
else
puts 'The following includes are not linked:'
includes.each do |include|
puts "No links for #{include}".yellow
end
puts "Found #{includes.size} unlinked includes".red
puts 'Be careful removing include files. Some include files, such as those in the layout/** directory, may not be linked in the project, but may be used implicitly by the doc theme.'.bold
end
end
end