Skip to content

Commit b9786d4

Browse files
Merge pull request rails#284 from jonathanhefner/main-page-fix-file-choice
Fix choice of main page
2 parents edb3ec8 + 15b2a5f commit b9786d4

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

Rakefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ class RailsTask < Rails::API::EdgeTask
4343
"doc/rails"
4444
end
4545

46+
# This file has been renamed in Rails 7.1.
47+
# TODO: Remove this override some time after Rails 7.1 is released.
4648
def api_main
47-
"rails/railties/RDOC_MAIN.md"
49+
super.sub("RDOC_MAIN.rdoc", "RDOC_MAIN.md")
4850
end
4951

5052
def component_root_dir(component)
51-
path = File.join("rails", component)
52-
return path
53+
File.join("rails", component)
5354
end
5455

5556
def setup_horo_variables

lib/sdoc/generator.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def initialize(store, options)
8787
end
8888
@options.pipe = true
8989

90+
@original_dir = Pathname.pwd
9091
@template_dir = Pathname.new(options.template_dir)
9192
@base_dir = options.root
9293

@@ -119,6 +120,14 @@ def file_dir
119120
FILE_DIR
120121
end
121122

123+
### Determines index page based on @options.main_page (or lack thereof)
124+
def index
125+
path = @original_dir.join(@options.main_page || @options.files.first || "")
126+
file = @files.find { |file| @options.root.join(file.full_name) == path }
127+
raise "Could not find main page #{path.to_s.inspect} among rendered files" if !file
128+
file
129+
end
130+
122131
protected
123132
### Output progress information if debugging is enabled
124133
def debug_msg( *msg )
@@ -202,25 +211,6 @@ def generate_class_tree_level(classes, visited = {})
202211
tree
203212
end
204213

205-
### Determines index page based on @options.main_page (or lack thereof)
206-
def index
207-
# Break early to avoid a big if block when no main page is specified
208-
default = @files.first
209-
return default unless @options.main_page
210-
211-
# TODO: Total hack to strip the source directory from the main page
212-
# Since the file list does not include the root source path
213-
clean_main = @options.main_page.gsub("rails/", "")
214-
215-
if file = @files.find { |f| f.full_name == clean_main }
216-
debug_msg "Found main at #{file}"
217-
file
218-
else
219-
debug_msg "Falling back to default main at #{default}"
220-
default
221-
end
222-
end
223-
224214
### Copy all the resource files to output dir
225215
def copy_resources
226216
resources_path = @template_dir + RESOURCES_DIR

spec/rdoc_generator_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,54 @@ def parse_options(*options)
7474
_(parse_options("--title", "Docs Docs Docs!").title).must_equal "Docs Docs Docs!"
7575
end
7676
end
77+
78+
describe "#index" do
79+
before do
80+
@dir = File.expand_path("../lib", __dir__)
81+
@files = ["sdoc.rb", "sdoc/version.rb"].sort.reverse
82+
end
83+
84+
it "defaults to the first --files value" do
85+
Dir.chdir(@dir) do
86+
sdoc = rdoc_dry_run("--files", *@files).generator
87+
_(sdoc.index.absolute_name).must_equal @files.first
88+
end
89+
end
90+
91+
it "raises when the default value is not a file" do
92+
sdoc = rdoc_dry_run("--files", @dir, "--exclude=(js|css|svg)$").generator
93+
error = _{ sdoc.index }.must_raise
94+
_(error.message).must_include @dir
95+
end
96+
97+
it "uses the value of --main" do
98+
Dir.chdir(@dir) do
99+
sdoc = rdoc_dry_run("--main", @files.first, "--files", *@files).generator
100+
_(sdoc.index.absolute_name).must_equal @files.first
101+
end
102+
end
103+
104+
it "raises when the main page is not among the rendered files" do
105+
Dir.chdir(@dir) do
106+
sdoc = rdoc_dry_run("--main", @files.first, "--files", @files.last).generator
107+
error = _{ sdoc.index }.must_raise
108+
_(error.message).must_include @files.first
109+
end
110+
end
111+
112+
it "works when --root is specified" do
113+
Dir.chdir(File.dirname(@dir)) do
114+
root = File.basename(@dir)
115+
@files.map! { |file| File.join(root, file) }
116+
sdoc = rdoc_dry_run("--root", root, "--main", @files.first, "--files", *@files).generator
117+
_(sdoc.index.absolute_name).must_equal @files.first
118+
end
119+
end
120+
121+
it "works with absolute paths" do
122+
@files.map! { |file| File.join(@dir, file) }
123+
sdoc = rdoc_dry_run("--main", @files.first, "--files", *@files).generator
124+
_(sdoc.index.absolute_name).must_equal @files.first
125+
end
126+
end
77127
end

spec/spec_helper.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ def with_env(env, &block)
1212
ENV.replace(original_env)
1313
end
1414

15+
def rdoc_dry_run(*options)
16+
RDoc::RDoc.new.tap do |rdoc|
17+
rdoc.document(%W[--dry-run --quiet --format=sdoc --template=rails] + options.flatten)
18+
end
19+
end
20+
1521
# Returns an RDoc::TopLevel instance for the given Ruby code.
1622
def rdoc_top_level_for(ruby_code)
1723
# RDoc has a lot of internal state that needs to be initialized. The most
1824
# foolproof way to initialize it is by simply running it with a dummy file.
19-
$rdoc_for_specs ||= RDoc::RDoc.new.tap do |rdoc|
20-
rdoc.document(%W[--dry-run --quiet --format=sdoc --template=rails --files #{__FILE__}])
21-
end
25+
$rdoc_for_specs ||= rdoc_dry_run("--files", __FILE__)
2226

2327
$rdoc_for_specs.store = RDoc::Store.new
2428

0 commit comments

Comments
 (0)