Skip to content
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

Asciidoctor: Copy images for inline image macro #736

Merged
merged 5 commits into from
Apr 1, 2019

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Mar 20, 2019

This copies images declared "inline". Asciidoctor doesn't give a good
way of hooking into these images so we have to scan for them when we
walk the tree. It isn't the best solution, but at least we don't have to
scan for them after the document is rendered! If we did that we wouldn't
get line number information when we can't find images....

Closes #730

This copies images declared "inline". Asciidoctor doesn't give a good
way of hooking into these images so we have to scan for them when we
walk the tree. It isn't the best solution, but at least we don't have to
scan for them after the document is rendered! If we did that we wouldn't
get line number information when we can't find images....

Closes elastic#730

##
# Convert an asciidoc string into docbook returning the result and any warnings.
def convert_with_logs(input, extra_attributes = {})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a much more rspec friendly way to check for logs. Though I still like having two flavors - one that automatically asserts that there weren't any logs so we don't have to have it didn't log anything assertions all over the place....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add an expectation using allow or expect. That way, you won't get an assertion it didn't log anything

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh! Let me do some research there.

@nik9000 nik9000 requested a review from estolfo March 20, 2019 14:01
include_examples 'copies images'
end
context 'for the image inline macro' do
# NOCMOMIT rename image_ref to target
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops. Looks like eI need to pick up these nocommits

Copy link
Contributor

@estolfo estolfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth looking into using expect or allow for the negative log assertions.
I've also made a few other Rspec-related comments.

end

def process_image(block, uri)
return if uri == ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any chance uri could be nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but I'll do return unless uri just because that feels a little more "ruby" to me.

@@ -6,7 +6,7 @@
require 'fileutils'
require 'tmpdir'

RSpec.describe CopyImages::CopyImages do
RSpec.describe CopyImages do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also just write describe without calling it on Rspec

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That causes an undefined method error for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, maybe your version of RSpec is older
Anyway, it doesn't really matter how you call describe

])
end
it 'logs that it copied the image' do
expect(logs).to eq("INFO: <stdin>: line #{include_line}: copying #{spec_dir}/resources/copy_images/example1.png")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected result is a bit long, so perhaps it can be put in a let block so that it's easier to parse. Also, if the expected result changes, it's easier to locate in the spec file what needs to edited.
For example:

let(:expected_copied_image) do
  [[resolved, "#{spec_dir}/resources/copy_images/example1.png"],]
end

it 'logs that it copied the image' do
  expect(logs).to eq(expected_copied_image)
end


##
# Convert an asciidoc string into docbook returning the result and any warnings.
def convert_with_logs(input, extra_attributes = {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add an expectation using allow or expect. That way, you won't get an assertion it didn't log anything

result
end
let(:logs) { result[1] }
let(:copied) { result[2] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this to be a little confusing. I think the variables you're interested in can be extracted out into their own let blocks and then the ! can be used to ensure that the result is created before other variables are accessed. I'm not sure if this would work, but I'm thinking something like this that isolates the variables.

let!(:logs) do
  attributes = copy_attributes copied
  (convert_with_logs input, attributes)[1]
end

let(:copied) do
  []
end

Is the copied object mutated by the copy_attributes method?

@nik9000
Copy link
Member Author

nik9000 commented Mar 29, 2019

@estolfo, I pushed something to switch from the tuple-returning method to a shared context that can be included that defines the things that I might assert on.

I haven't done anything about logging so if there aren't any logging assertions then logs are ignored, but that seems fine for this change. I can handle it in a follow up I think.

Copy link
Contributor

@estolfo estolfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the updated way to test the logs!

.map { |l| "#{l[:severity]}: #{l[:message].inspect}" }
.join("\n")
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! This looks good and more rspec-y

@nik9000 nik9000 merged commit 1a12e19 into elastic:master Apr 1, 2019
@nik9000
Copy link
Member Author

nik9000 commented Apr 1, 2019

Thanks for reviewing @estolfo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants