Skip to content

Commit 883cbcc

Browse files
committed
Rewrite Linter test for a valid page
This starts moving away from fixture based tests towards tests that are completely defined in the test files. Also add a test helper for the Linter tests.
1 parent 8594939 commit 883cbcc

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ require "rake/testtask"
133133
Rake::TestTask.new(:"test-linter") do |t|
134134
t.description = "Run tests for the Linter library"
135135
t.libs = ["test", "lib"]
136-
t.test_files = ["test/test_linter.rb"]
136+
t.test_files = ["test/test_linter.rb"] + FileList['test/test_linter_*.rb']
137137
t.verbose = true
138138
end

test/helper.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require "minitest/autorun"
4+
require "fileutils"
5+
require "pathname"
6+
7+
8+
TEST_DIR = File.expand_path(__dir__)
9+
TEMP_DIR = File.join(TEST_DIR, "tmp")
10+
11+
12+
def setup_tempdir
13+
FileUtils.mkdir_p(TEMP_DIR)
14+
15+
File.exist?(TEMP_DIR) ? TEMP_DIR : nil
16+
end
17+
18+
def chdir_tempdir
19+
setup_tempdir unless File.exist?(TEMP_DIR)
20+
Dir.chdir(TEMP_DIR)
21+
end
22+
23+
def teardown_tempdir
24+
FileUtils.rm_rf(TEMP_DIR) if File.exist?(TEMP_DIR)
25+
end
26+
27+
def create_releases_file
28+
FileUtils.mkdir_p("_data")
29+
FileUtils.touch("_data/releases.yml")
30+
end
31+
32+
def create_file(path, content)
33+
raise "path must be relative" unless Pathname.new(path).relative?
34+
35+
dir = File.dirname(path)
36+
FileUtils.mkdir_p(dir)
37+
File.open(path, "w") {|f| f.write content }
38+
end
39+
40+
def linter_output
41+
stdout, _stderr = capture_io { Linter.new(exit_on_errors: false).run }
42+
43+
stdout
44+
end

test/test_linter_valid_page.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require "helper"
4+
require "linter"
5+
6+
7+
describe Linter do
8+
9+
before do
10+
chdir_tempdir
11+
create_releases_file
12+
13+
@ok = "Checking markdown files... ok\n"
14+
end
15+
16+
after do
17+
teardown_tempdir
18+
end
19+
20+
it "checks ok a valid page" do
21+
content = <<~PAGE
22+
---
23+
layout: page
24+
title: "Page"
25+
lang: en
26+
---
27+
28+
Content
29+
PAGE
30+
31+
create_file("en/page.md", content)
32+
_(linter_output).must_equal @ok
33+
end
34+
end

0 commit comments

Comments
 (0)