krober10nd / SeismicMesh

2D/3D serial and parallel triangular mesh generation tool for finite element methods.
https://seismicmesh.readthedocs.io/
GNU General Public License v3.0
127 stars 32 forks source link

exdown -> pytest-codeblocks #213

Closed krober10nd closed 3 years ago

krober10nd commented 3 years ago
krober10nd commented 3 years ago

hey @nschloe any help on how I could rewrite my README test here (https://github.com/krober10nd/SeismicMesh/blob/master/tests/test_README.py) to use now pytest-codeblocks?

krober10nd commented 3 years ago

I think extract becomes extract_from_file and the package name changes.

nschloe commented 3 years ago

For your use case, pytest-codeblocks needs pre and post blocks -- will add this soon.

nschloe commented 3 years ago

It already works with hidden code blocks, see https://github.com/nschloe/pytest-codeblocks#merging-code-blocks.

krober10nd commented 3 years ago

Sorry Nico, I didn't follow what you meant exactly. I did take a look at the tests for pytest-codeblocks and in these tests, you have this input parameter testdir that is used to execute the *.md files that are generated in the scripts.

I can easily load the README into a list but not sure how to get the testdir parameter into the mix.

   import gzip
   import pathlib
   import shutil

   import exdown
   import pytest
   import pytest_codeblocks
   import requests

   this_dir = pathlib.Path(__file__).resolve().parent

   @pytest.mark.serial
   def test_readme():

       # download
        url = "http://s3.amazonaws.com/open.source.geoscience/open_data/bpvelanal2004/vel_z6.25m_x12.5m_exact.segy.gz"
          filename = url.split("/")[-1]
        with open(filename, "wb") as f:
             r = requests.get(url)
             f.write(r.content)

       # un-compress
        with gzip.open("vel_z6.25m_x12.5m_exact.segy.gz", "r") as f_in, open(
           "vel_z6.25m_x12.5m_exact.segy", "wb"
        ) as f_out:
           shutil.copyfileobj(f_in, f_out)
       lst = pytest_codeblocks.extract_from_file(this_dir.parent / "README.md")
       print(lst)
       testdir.makefile(".md", lst)
       result = testdir.runpytest("--codeblocks")
       result.assert_outcomes(passed=1)
nschloe commented 3 years ago

You don't need a separate test file. pytest README.md should already test all snippets in the file. (After installing pytest-codeblocks.)

krober10nd commented 3 years ago

hmm I have pytest-codeblocks installed

(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ pip show pytest-codeblocks
Name: pytest-codeblocks
Version: 0.11.1
Summary: Test code blocks in your READMEs
Home-page: https://github.com/nschloe/pytest-codeblocks
Author: Nico Schlömer
Author-email: nico.schloemer@gmail.com
License: MIT
Location: /Users/Keith/Desktop/PostDoctoralWork/codes/firedrake/lib/python3.7/site-packages
Requires: pytest
Required-by: 

but when I run

 pytest README.md

I get

(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ pytest README.md 
================================================================= test session starts =================================================================
platform darwin -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/Keith/junk/SeismicMesh, configfile: pytest.ini
plugins: xdist-2.2.1, forked-1.3.0, codeblocks-0.11.1
collected 0 items                                                                                                                                     

================================================================ no tests ran in 0.01s ================================================================
ERROR: not found: /Users/Keith/junk/SeismicMesh/README.md
(no name '/Users/Keith/junk/SeismicMesh/README.md' in any of [])
nschloe commented 3 years ago

And the file exists?

krober10nd commented 3 years ago

Yep.

(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ ls -lt README.md 
-rw-r--r--  1 Keith  staff  31201 May 25 11:25 README.md
(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ pip show pytest-codeblocks
Name: pytest-codeblocks
Version: 0.11.1
Summary: Test code blocks in your READMEs
Home-page: https://github.com/nschloe/pytest-codeblocks
Author: Nico Schlömer
Author-email: nico.schloemer@gmail.com
License: MIT
Location: /Users/Keith/Desktop/PostDoctoralWork/codes/firedrake/lib/python3.7/site-packages
Requires: pytest
Required-by: 
(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ pytest README.md 
================================================================= test session starts =================================================================
platform darwin -- Python 3.7.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/Keith/junk/SeismicMesh, configfile: pytest.ini
plugins: xdist-2.2.1, forked-1.3.0, codeblocks-0.11.1
collected 0 items                                                                                                                                     

================================================================ no tests ran in 0.01s ================================================================
ERROR: not found: /Users/Keith/junk/SeismicMesh/README.md
(no name '/Users/Keith/junk/SeismicMesh/README.md' in any of [])

(firedrake) (base) keiths-MacBook-Pro:SeismicMesh Keith$ 
nschloe commented 3 years ago

No idea what's going wrong then. Perhaps xdist or forked is messing with pytest-codeblocks?

nschloe commented 3 years ago

Ah wait, I can reproduce it. Let me dig a little further...

nschloe commented 3 years ago

I forgot, you need --codeblocks to turn the feature on.

krober10nd commented 3 years ago

Well progress at least but for whatever reason it's trying to install gmsh now when running

pytest --codeblocks README.md

although gmsh is not a dependency for SeismicMesh. Maybe it's finding the benchmarks/README.md?

nschloe commented 3 years ago

pytest-codeblock never installs anything. Do you have

```sh
pip install something
in your readme? It will test this too. You can skip with
krober10nd commented 3 years ago

Got it. Just had some old junk folders inside my working directory that it was picking up. I just call the commandline from a Python script now to unit test. Simplifies things a lot. https://github.com/krober10nd/SeismicMesh/blob/7907ac91522fef2492a20019c68cc1a845babb75/tests/test_README.py#L28

nschloe commented 3 years ago

Holy Barnabas! This is like a child giving birth to its own mother. But okay, if it works, it works.

krober10nd commented 3 years ago

Holy Barnabas! This is like a child giving birth to its own mother. But okay, if it works, it works.

lol I am technically an "engineer" by training. I specialize in rube goldberg machines. :]