Skip to content

Commit 7461c20

Browse files
authored
Merge pull request #73 from coderdojo-japan/merge-test-suites-to-html-proofer
Merge test-directory-structure test into HTML Proofer's custom checks and run it in CI
2 parents c554734 + d98623f commit 7461c20

File tree

4 files changed

+58
-84
lines changed

4 files changed

+58
-84
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ jobs:
3232
run: |
3333
JEKYLL_ENV=production bundle exec jekyll build
3434
JEKYLL_ENV=production bundle exec jekyll doctor
35-
# SKIP_BUILD=true bundle exec rake test
36-
# NOTE: サイトが仕上がったら、上記テストを走らせると自動検知できる。
37-
# ただ初期は自動検知の通知が多すぎるので手動で実行するのが吉。
38-
python test-directory-structure.py
35+
SKIP_BUILD=true bundle exec rake test
3936
4037
# Deploy job is triggered only pushed to main branch && CI passed
4138
deploy:

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ task default: 'test'
33
# HTML Proofer helps to detect broken links.
44
# https://github.com/gjtorikian/html-proofer
55
require 'html-proofer'
6+
require_relative '_tests/custom_checks'
7+
68
task test: [:build] do
79
options = {
8-
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon'],
10+
checks: ['Links', 'Images', 'Scripts', 'OpenGraph', 'Favicon', 'CustomChecks'],
911
allow_hash_href: false,
1012
disable_external: true,
1113
enforce_https: true,

_tests/custom_checks.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# See the following 'Custom Tests' section to add tests ;)
2+
# https://github.com/gjtorikian/html-proofer#custom-tests
3+
4+
require 'json'
5+
require 'yaml'
6+
7+
class CustomChecks < ::HTMLProofer::Check
8+
BASE_PATH = '_site'
9+
10+
def run
11+
current_filename = @runner.current_filename
12+
puts "\tchecking ... " + current_filename[5..].split('.').first
13+
14+
check_directory_structure
15+
end
16+
17+
# Check directory structure to ensure all pages are generated as index.html
18+
# This ensures proper routing for both /foobar/ and /foobar pathes.
19+
def check_directory_structure
20+
allowed_names = ['index.html', '404.html']
21+
current_file = @runner.current_filename
22+
23+
# Only check HTML files
24+
return unless current_file.end_with?('.html')
25+
26+
# Get the filename
27+
filename = File.basename(current_file)
28+
29+
# Skip if allowed filenames or redirect files
30+
return if allowed_names.include?(filename)
31+
return if is_redirect_file?(current_file)
32+
33+
# Report failure for non-index HTML files
34+
add_failure(
35+
<<~ERROR_MESSAGE
36+
index.html でない HTML ファイルが生成されました:
37+
\s File: #{current_file}
38+
\s すべてのページは index.html として生成される必要があります。
39+
\s これにより /path/ と /path の両方でルーティングが可能になります。
40+
ERROR_MESSAGE
41+
)
42+
end
43+
44+
# Check if it's a redirect or not by meta tags
45+
def is_redirect_file?(filepath)
46+
return false unless File.exist?(filepath)
47+
48+
content = File.read(filepath)
49+
# Check for meta refresh redirect tag
50+
content.include?('<meta http-equiv="refresh"') ||
51+
content.include?("<meta http-equiv='refresh'") ||
52+
content.include?('<meta http-equiv=refresh')
53+
end
54+
end

test-directory-structure.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)