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-Doctest Testing #116

Merged
merged 30 commits into from
Aug 29, 2017
Merged

Asciidoctor-Doctest Testing #116

merged 30 commits into from
Aug 29, 2017

Conversation

obilodeau
Copy link
Member

@obilodeau obilodeau commented Oct 27, 2016

The beginning of asciidoctor-doctest integration (#92).

At first, I thought this would help us test both slim and jade at the same time but I realize it would need a asciidoctor-doctest.js implementation. Maybe we could add a rake task to generate templates from the javascript/node toolchain and use doctest to do the diff. @Mogztter thoughts on this?

Test samples were generated from current back-end so I think some thought, reviews and more custom examples should be migrated before calling it a go.

Also Travis integration would be really nice.

Tasks

Comments and further contributions welcome!

@ggrossetie
Copy link
Member

At first, I thought this would help us test both slim and jade at the same time but I realize it would need a asciidoctor-doctest.js implementation. Maybe we could add a rake task to generate templates from the javascript/node toolchain and use doctest to do the diff. @Mogztter thoughts on this?

How did you generate files in output (ie. where is the source files) ? input directory is empty, is this intended ?

@obilodeau
Copy link
Member Author

How did you generate files in output (ie. where is the source files) ? input directory is empty, is this intended ?

Yes it is normal. The files in output/ match tests in asciidoctor-doctest's own test set: https://github.com/asciidoctor/asciidoctor-doctest/tree/master/data/examples/asciidoc. Eventually, I'll migrate the manual tests over to input/ which will need new matching results in output/ but I haven't done so yet.

@ggrossetie
Copy link
Member

Ok so I need to generate one file per example in JavaScript to mimic what Asciidoctor Doctest is doing ?
Can we copy data/examples/asciidoc files over ? It will be easier to read example files.

Currently output directory is what we get now when we are using Slim template or what the expected result should be ? Where should I put the result generated from Jade template ? Should we consider Slim template the reference ?

Thanks
Guillaume

@mojavelinux
Copy link
Member

The sooner we can get this merged, the better. Then we can all work on improving the examples. wdyt?

@ggrossetie
Copy link
Member

I'm starting to upgrade Jade/Pug templates to use the new public API with Asciidoctor.js 1.5.6+.

To make sure that I'm not breaking anything, I'm writing a test for each .adoc file in the test/ directory. I'm guessing that maybe we should merge this pull request so I could use doctest in the JavaScrip tests suite ?

@obilodeau What's left to do ?

@obilodeau
Copy link
Member Author

I made a custom test working today. See test/input/document.adoc.

The manual tests are not ported to the doctest setup and I'm still trying to workaround the fact that all my tests would need to be in a very large document.adoc.

What would I need to do to test using the javascript toolchain?

@obilodeau
Copy link
Member Author

[...] I'm still trying to workaround the fact that all my tests would need to be in a very large document.adoc.

Turned out this was an assumption I made. I am converting the tests to doctest right now (adding metadata). Making some reorganization while I'm at it. But now I need to get some 🛌.

So all test/*.adoc files should have slim renders and be compared when running bundle exec rake test pretty soon.

Hopefully, that rendering will be stable enough between jade and slim so that we can use two different templating engine but only one HTML result set. We'll see soon enough.

In the mean time, anything (instructions) that can help me run rake tests using the javascript toolchain would be appreciated 😄

@obilodeau obilodeau self-assigned this Aug 19, 2017
@obilodeau obilodeau added this to the 1.1.0 milestone Aug 19, 2017
@ggrossetie
Copy link
Member

I think it won't be easy to cross compile asciidoctor-doctest to JavaScript because the library has a few dependencies.
Maybe we could mimic the behavior of doctest in JavaScript to generate tests ?

The basic idea is to compare an input with an expected output:

var asciidoctor = require('asciidoctor.js')();
require('asciidoctor-template.js');
require('../build/asciidoctor-revealjs.js');
var assert = require('assert');
var fs = require('fs');

describe('Rendering', function () {

  var tests = [
    {input: './test/block_admonition.adoc', expected: './test/block_admonition_output.html'},
    {input: './test/block_audio.adoc', expected: './test/block_audio_output.html'},
    {input: '...', expected: '...'}
  ];

  tests.forEach(function (test) {
    it('should produce a valid Reveal.js presentation for ' + test.input, function () {
      var html = asciidoctor.convertFile(test.input, {safe: 'safe', backend: 'revealjs', to_file: false});
      assert.equal(html, fs.readFileSync(test.expected, 'utf-8'));
    });
  });
});

@obilodeau
Copy link
Member Author

Maybe we could mimic the behavior of doctest in JavaScript to generate tests ?

The problem here is that we won't leverage doctest's power unless we reimplement it the same. Features like showing exactly where the problem is introduced with colorized diffs. Also, the output will probably be different since it will use another HTML beautifier and I'm afraid we won't be able to use the same test output for both slim and jade (ensuring compatibility between the two).

That said, I think this is the only real option we've got for now unless asciidoctor-doctest's maintainer is interested in making it transcompilable.

@ggrossetie
Copy link
Member

The problem here is that we won't leverage doctest's power unless we reimplement it the same. Features like showing exactly where the problem is introduced with colorized diffs. Also, the output will probably be different since it will use another HTML beautifier and I'm afraid we won't be able to use the same test output for both slim and jade (ensuring compatibility between the two).

That's right, you will have the diff output of the chosen JavaScript framework.
But the output is not that bad (at least with mocha):

mocha-html-output

* test cases are now in examples/ which should make them easier to discover
* rendered tests are in test/output/slim so we can have jade/ at some point
* most test cases are not useful right now due to issue: asciidoctor/asciidoctor-doctest#12
@obilodeau
Copy link
Member Author

So all test/*.adoc files should have slim renders and be compared when running bundle exec rake test pretty soon.

That's done. I also moved the test cases to examples/ as I find it easier to discover for users. Using the doctest toolchain, nothing would prevent us to have both examples/*.adoc files for complete examples and test/*.adoc for corner-case or regression tests.

Because of asciidoctor/asciidoctor-doctest#12 most tests are useless now.

Next up is travis-ci integration (running rake test on github pushes)

@jirutka
Copy link
Member

jirutka commented Aug 19, 2017

@obilodeau #116 (comment):

it would need a asciidoctor-doctest.js implementation

No, you don’t need. Doctest doesn’t need any deep integration with Asciidoctor, it can be modified to just call external program (asciidoctor.js) that accepts arguments and dumps output to stdout.

@Mogztter #116 (comment):

Ok so I need to generate one file per example in JavaScript to mimic what Asciidoctor Doctest is doing ?

No, one file may contain multiple examples, typically relevant to one particular AST node (e.g. inline_anchor, olist, …). It’s all described here.

@Mogztter #116 (comment):

Can we copy data/examples/asciidoc files over ? It will be easier to read example files.

You can, but really shouldn’t. It’d be just code duplication with all its drawbacks.

@obilodeau #116 (comment):

I'm still trying to workaround the fact that all my tests would need to be in a very large document.adoc.

Why? This is not how it should be used.

@obilodeau #116 (comment):

Hopefully, that rendering will be stable enough between jade and slim so that we can use two different templating engine but only one HTML result set. We'll see soon enough.

Doctest “normalizes” HTML for comparison, so if both templates produce syntactically equal output (formatting, attributes order etc. is irrelevant), then they will be equal.

Why do you need two sets of templates in two different templating engines? Asciidoctor.js is Asciidoctor transpiled to JS using Opal, right? Then why not to transpile even templates, specifically Ruby code generated by the template engine?

You can use asciidoctor-templates-compiler to compile set of Slim templates into single Ruby file – Asciidoctor converter. When you avoid using Slim’s splat attributes (and maybe some other extra extensions), then the resulting code is standalone, i.e. doesn’t depend on Slim or Temple at all. I wrote it for asciidoctor-html5s.

@Mogztter #116 (comment):

I think it won't be easy to cross compile asciidoctor-doctest to JavaScript because the library has a few dependencies.

Why you would do that? That’s not needed at all.

@obilodeau #116 (comment):

That said, I think this is the only real option we've got for now unless asciidoctor-doctest's maintainer is interested in making it transcompilable.

I’m not gonna make doctest transcompilable, because it doesn’t make any sense for me. As I know, Asciidoctor.js runs on node.js, so it should be possible to call it externally as a command, pass it some arguments and gather HTML on stdout, shouldn’t be? That’s all what’s needed for integration with Doctest. It currently does not support this out-of-box, but I’d be happy to implement it.

@jirutka
Copy link
Member

jirutka commented Aug 19, 2017

BTW, please use asciidoctor-doctest 2.0.0.beta.3 or dev branch, not the last stable or master branch. I’ve started almost complete rewrite/refactoring of doctest two years ago and came back to it few weeks ago. The new version is much easier to integrate into project and also more cleanly extensible.

@obilodeau
Copy link
Member Author

obilodeau commented Aug 19, 2017

Thanks @jirutka for the detailed answers.

Why do you need two sets of templates in two different templating engines? Asciidoctor.js is Asciidoctor transpiled to JS using Opal, right? Then why not to transpile even templates, specifically Ruby code generated by the template engine?

You can use asciidoctor-templates-compiler to compile set of Slim templates into single Ruby file – Asciidoctor converter. When you avoid using Slim’s splat attributes (and maybe some other extra extensions), then the resulting code is standalone, i.e. doesn’t depend on Slim or Temple at all. I wrote it for asciidoctor-html5s.

I think this right here is what we should evaluate first.

As I know, Asciidoctor.js runs on node.js, so it should be possible to call it externally as a command, pass it some arguments and gather HTML on stdout, shouldn’t be? That’s all what’s needed for integration with Doctest. It currently does not support this out-of-box, but I’d be happy to implement it.

This would be an acceptable compromise if we can't get compiled templates to work.

@mojavelinux, @Mogztter: thoughts?

BTW, please use asciidoctor-doctest 2.0.0.beta.3 or dev branch, not the last stable or master branch. I’ve started almost complete rewrite of doctest two years ago and came back to it few weeks ago. The new version is much easier to integrate into project and also more cleanly extensible.

Ok, I will update this PR so that we use 2.0.0.beta3.

Edit: I updated task list at the top of the PR to reflect current status.

@@ -29,4 +29,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'thread_safe', '~> 0.3.5'

s.add_development_dependency 'rake', '~> 10.4.2'
s.add_development_dependency 'asciidoctor-doctest', '~> 2.0.0.beta.4'
Copy link
Member

Choose a reason for hiding this comment

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

I’d be better to fix this version (= 2.0.0.beta.4), ’cause it’s a beta and there may come some incompatible changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a cybersecurity person and I think it's always better to enable upstream projects to patch their issues but I see your point. I'll pin to that version but I hope you'll keep us in the loop for improvements / security issues 😉

<!-- .basic -->
<div class="audioblock">
<div class="content">
<audio controls="" src="ocean_waves.mp3">Your browser does not support the audio tag.</audio>
Copy link
Member

@jirutka jirutka Aug 21, 2017

Choose a reason for hiding this comment

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

There should be controls, without ="" (and the same situation with loop and some other attributes). This is problem with nokogiri, it doesn’t not support HTML5 well. :( However, you can manually fix it in generated examples, it can parse it well and assert passes. It’s not a big deal though.

Copy link
Member Author

Choose a reason for hiding this comment

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

File no longer exists after the refresh

Copy link
Member Author

Choose a reason for hiding this comment

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

Aaah it's named audio.html now. I fixed it. Commit coming soon.

</table>
</div>

<!-- .basic_multiline -->
Copy link
Member

Choose a reason for hiding this comment

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

This file is outdated, I changed names format in beta.3 to use hyphen instead of underscore.

Copy link
Member

@jirutka jirutka Aug 21, 2017

Choose a reason for hiding this comment

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

You can use the following to update them:

sed -Ei '/^<!-- \.(\w+) -->/ s/_/-/g' *.html
sed -Ei '/^<!-- \.(\w+)$/ s/_/-/g' *.html

However, there are more changes in admonition examples, so better to generate them from scratch

Copy link
Member

Choose a reason for hiding this comment

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

Ah, and also I’ve dropped block_ prefix in beta.3, to be consistent with Asciidoctor (IIRC block_ prefix in node names is a legacy).

@@ -0,0 +1,2194 @@
<!-- .title -->
Copy link
Member

@jirutka jirutka Aug 21, 2017

Choose a reason for hiding this comment

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

Document examples should be scoped (using :include: option), look at https://github.com/jirutka/asciidoctor-html5s/blob/master/test/examples/html5/document.html for inspiration. (Just add :include: options and run doctest:generate FORCE=y to update them.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see now why the options were coming from the output document! Because I have no control on the input ones. Makes sense. Thanks for the pointer!


<!-- .link-with-target-blank -->

<!-- .link-with-role -->
Copy link
Member

Choose a reason for hiding this comment

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

These empty examples don’t look good… some error in reveal.js converter…?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, it seems that you’ve somehow mixed examples generated using older DocTest version and the latest. It’d be better/easier to remove all output examples generated from reference examples and generate them from scratch.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just done that

Copy link
Member Author

Choose a reason for hiding this comment

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

However, the inline_anchor.html template you mentioned just got re-generated the same way.

@obilodeau
Copy link
Member Author

@jirutka: I addressed all your comments.

Rakefile Outdated
t.converter_opts = { template_dirs: 'templates/slim' }
end

# TODO add a rake task to render fully working examples to look at them in a browser
Copy link
Member

Choose a reason for hiding this comment

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

We should maybe create an issue to not forget to do it 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't you always grep for TODO|FIXME before you do a release? ;)

Copy link
Member

Choose a reason for hiding this comment

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

No, do you ? I grep from time to time but it's not integrated in my development process.
That's why I like to create issues on GitHub so I can keep track of what needs to be done.

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented in e5fda00

@ggrossetie
Copy link
Member

I'm not an expert here but this looks fine to me. Good work 👍

@jirutka
Copy link
Member

jirutka commented Aug 25, 2017

There’s a lot of skipped examples (i.e. missing output examples), is it okay…? Run bundle exec rake doctest:test VERBOSE=yes to see which.

@obilodeau
Copy link
Member Author

I think skipped examples are fine for now. We can open a compliance issue about them but I would put them out of scope for what we are trying to achieve here.

Rakefile Outdated
# :base_dir => 'examples'
## :template_dir => 'templates/slim'
system "bundle exec asciidoctor-revealjs data-background-oldstyle.adoc", :chdir => 'examples'
end
Copy link
Member Author

Choose a reason for hiding this comment

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

@Mogztter: I didn't manage to make convert_file work. base_dir was not honored. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing that you may have to configure the safe mode.

Copy link
Member

Choose a reason for hiding this comment

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

The following seems to work:

Asciidoctor.convert_file 'data-background-newstyle.adoc',
        :safe => 'safe',
        :backend => 'revealjs',
        :base_dir => 'examples'

Copy link
Member Author

Choose a reason for hiding this comment

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

Still not working for me:

$ bundle exec rake render 
rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - data-background-newstyle.adoc
/home/olivier/src/asciidoc/asciidoctor-reveal.js/.bundle/gems/ruby/2.4.0/gems/asciidoctor-1.5.6.1/lib/asciidoctor.rb:1575:in `initialize'
/home/olivier/src/asciidoc/asciidoctor-reveal.js/.bundle/gems/ruby/2.4.0/gems/asciidoctor-1.5.6.1/lib/asciidoctor.rb:1575:in `open'
/home/olivier/src/asciidoc/asciidoctor-reveal.js/.bundle/gems/ruby/2.4.0/gems/asciidoctor-1.5.6.1/lib/asciidoctor.rb:1575:in `convert_file'
/home/olivier/src/asciidoc/asciidoctor-reveal.js/Rakefile:19:in `block in <top (required)>'
/usr/bin/bundle:22:in `load'
/usr/bin/bundle:22:in `<main>'
Tasks: TOP => render
(See full trace by running task with --trace)

Should I change cwd with Rake? Anyway, I want the output to be in examples/ and gitignore examples/reveal.js/.

Copy link
Member

Choose a reason for hiding this comment

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

Uh oh I see... I didn't run the right Rake task 😳
convert_file method does not use the base_dir attribute so you'll have to use the full path (see asciidoctor.rb#convert_file)

What about:

Asciidoctor.convert_file 'examples/data-background-newstyle.adoc',
        :safe => 'safe',
        :backend => 'revealjs',
        :base_dir => 'examples'

to_dir should be relative to base_dir so the document should be rendered in the examples/ directory.

Copy link
Member

Choose a reason for hiding this comment

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

If you want the document to be rendered in examples/reveal.js/, I think you need to define :to_dir => 'reveal.js'.

@obilodeau
Copy link
Member Author

@jirutka I filed #134 for the skipped tests. I'm merging this now.

Thanks everyone for your patience and help through this! It should help development and avoid regressions a lot! 😄

@obilodeau obilodeau merged commit d5f3dce into asciidoctor:master Aug 29, 2017
@obilodeau obilodeau deleted the doctest branch August 29, 2017 03:11
@jirutka
Copy link
Member

jirutka commented Aug 29, 2017

Please next time rebase PR before merging, i.e. remove fixup commits and other noise to get more clean repo history.

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

Successfully merging this pull request may close these issues.

None yet

4 participants