New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot access site config from specs #1236
Comments
Hi, First of all, please note that this touches internal APIs, which are not supported and can (and will) break from time to time. Would it be possible for your code to access |
Hi, Yes, I know that I am touching internal types. 4.8.10 breaks other APIs (e.g. As we discussed earlier, I'm not a fan of polluting global objects (even if it is just the As it stands the issue only arises in tests, but from the diff I couldn't find the location where it broke, otherwise I would have submitted a PR. To answer your question, I could go through all my layouts and content files and replace |
Your approach of modifying internal classes is problematic for two reasons:
I strongly recommend not modifying existing classes. In your case, prefer
Everything inside |
OK, I'm going to investigate this. The first spec should check whether a forwarded item (via frontmatter) yields the correct target URL. The idea is:
def absolute_url_of(item)
append = item.path
if item.attributes[:forward]
append = @items[item.attributes[:forward]].path
end
@config[:base_url] + append
end
describe 'absolute URL' do
include Nanoc::Spec::HelperHelper
let(:item) { ctx.items['/index.html'] }
let(:target) { ctx.items['/somewhere/else.html'] }
before do
ctx.create_item('item', { forward: '/somewhere/else.html' }, '/index.html')
.create_item('target', {}, '/somewhere/else.html')
.create_rep(item, '/path/to' + item.identifier.to_s)
.create_rep(target, '/path/to' + target.identifier.to_s)
end
it 'yields URL of forwarded item' do
ctx.instance_exec(item, target) do |item, target|
url = absolute_url_of(item)
target = absolute_url_of(target)
expect(url).to eq(target)
end
end
end Is this the idea behind If so, how would I work around this?
|
There’s a few changes that you’ll need:
|
Also, prefer |
Full spec that works: module AbsoluteURLHelper
def absolute_url_of(item)
append =
if item[:forward]
@items[item[:forward]].path
else
item.path
end
@config[:base_url] + append
end
end
describe AbsoluteURLHelper, helper: true do
let(:item) { ctx.items['/index.html'] }
let(:target) { ctx.items['/somewhere/else.html'] }
before do
ctx.config[:base_url] = '/root'
ctx
.create_item('item', { forward: '/somewhere/else.html' }, '/index.html')
.create_item('target', {}, '/somewhere/else.html')
.create_rep(item, '/path/to' + item.identifier.to_s)
.create_rep(target, '/path/to' + target.identifier.to_s)
end
it 'yields URL of forwarded item' do
expect(helper.absolute_url_of(item)).to eq('/root/path/to/somewhere/else.html')
expect(helper.absolute_url_of(target)).to eq('/root/path/to/somewhere/else.html')
end
end |
You can leave the |
Thanks, that works. I see I got a bug by reassigning
Does nanoc hook into rspec for sites? It may work in the nanoc repo itself, but this has spec_helper.rb with |
If You can also opt to not use |
I need to access the site config in some of my nanoc extensions. These extensions cannot access
@context
directly, but use thecompilation_context
attached to theCompilationItemView
. The feature breaks when updating from 4.8.9 to 4.8.10, but only in specs. At runtime the site config is accessible.Steps to reproduce
The following test succeeds with nanoc 4.8.9 and fails with 4.8.10.
The text was updated successfully, but these errors were encountered: