├── src ├── __init__.py ├── helloworld.sh ├── helloworld.py └── mul.py ├── test_src ├── __init__.py ├── test_mul.py └── test_helloworld.py ├── .gitignore ├── .github └── workflows │ └── readme-test.yml ├── LICENSE └── README.md /src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test_src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | env 2 | .pytest_cache 3 | __pycache__ 4 | -------------------------------------------------------------------------------- /src/helloworld.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "hello" -------------------------------------------------------------------------------- /src/helloworld.py: -------------------------------------------------------------------------------- 1 | def hello(): 2 | return "v1.0.0 world" 3 | -------------------------------------------------------------------------------- /src/mul.py: -------------------------------------------------------------------------------- 1 | from math import pow 2 | 3 | def multiple(x): 4 | return pow(x, 2) -------------------------------------------------------------------------------- /test_src/test_mul.py: -------------------------------------------------------------------------------- 1 | from src.mul import multiple 2 | def test_mul(): 3 | assert multiple(3) == 9. -------------------------------------------------------------------------------- /test_src/test_helloworld.py: -------------------------------------------------------------------------------- 1 | from src.helloworld import hello 2 | 3 | 4 | def test_hello(): 5 | assert hello() == "v1.0.0 world" 6 | -------------------------------------------------------------------------------- /.github/workflows/readme-test.yml: -------------------------------------------------------------------------------- 1 | name: Test code and embed into README 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | jobs: 8 | readme-test: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | with: 13 | persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token 14 | fetch-depth: 0 # otherwise, you will failed to push refs to dest repo 15 | ref: refs/heads/${{ github.head_ref }} 16 | 17 | - name: Set up Python 18 | uses: actions/setup-python@v2 19 | with: 20 | python-version: 3.7 21 | - name: Install dependencies 22 | run: pip install pytest 23 | - name: Test with pytest 24 | run: pytest test_src 25 | 26 | - name: embedding readme 27 | uses: tokusumi/markdown-embed-code@v1.0.0 28 | with: 29 | markdown: "README.md" 30 | token: ${{ secrets.GITHUB_TOKEN }} 31 | message: "auto commit for embedding" 32 | no_change: "No change on Readme." 33 | silent: false 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 T. Tokusumi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Readme code testing example 2 | 3 | ![Test code and embed into README](https://github.com/tokusumi/readme-code-testing/workflows/Test%20code%20and%20embed%20into%20README/badge.svg) 4 | 5 | This repo demonstrates [Markdown Embed Code From File](https://github.com/marketplace/actions/markdown-embed-code-from-file) (GitHub Action). 6 | 7 | This would helps testing for your code within readme (actually for markdown). 8 | 9 | You could put code in external file and test them, as same as ordinary testing your code. You don't need copy & paste code and feel anxisous about compatibility anymore. 10 | 11 | ## Mark your code for embedding 12 | 13 | This action could inspect your code, if you add a file path in code block as "\`\`\`lang:external/file/path.py\`\`\`". 14 | 15 | The following code block has a file path `src/helloworld.py`: 16 | 17 | ```python:src/helloworld.py 18 | def hello(): 19 | return "v1.0.0 world" 20 | 21 | ``` 22 | 23 | See [src/helloworld.py](./src/helloworld.py) as: 24 | 25 | * Synchronized code. 26 | * Highlighting code in readme. 27 | * Not affected code rendering. 28 | 29 | And notice that there are nothing to do re-embedding new/modified code. This action overrides code automatically if you change code in external file and commit it. 30 | 31 | ### Try it 32 | 33 | Let's try demonstration in your repository as follows: 34 | 35 | 1. fork this repository. 36 | 1. edit [src/helloworld.py](./src/helloworld.py), create a new branch and start a pull request. 37 | 1. go to PR to check action result. 38 | 1. you can see auto updated readme at new branch you created above. (see if [above code block](#mark-your-code-for-embedding) is modified) 39 | 1. you can try again, if you edit it and commit again. 40 | 41 | ## More features 42 | ### Embedding specific lines 43 | 44 | You might add specific lines from one file (ex. [src/mul.py](./src/mul.py)). 45 | 46 | This action supports this with the syntax as "\`\`\`lang:external/file/path.py [start:end]\`\`\`" for it: 47 | 48 | ```py:src/mul.py [3-4] 49 | def multiple(x): 50 | return pow(x, 2) 51 | ``` 52 | 53 | ### Multiple use 54 | 55 | You might add one file path (ex. [src/mul.py](./src/mul.py)) for multiple code blocks: 56 | 57 | ```py:src/mul.py 58 | from math import pow 59 | 60 | def multiple(x): 61 | return pow(x, 2) 62 | ``` 63 | 64 | It works!: 65 | 66 | ```py:src/mul.py 67 | from math import pow 68 | 69 | def multiple(x): 70 | return pow(x, 2) 71 | ``` 72 | 73 | However, the other is not available. 74 | 75 | ### Non-existent file 76 | 77 | Notice that this action goes to "fail" if a file you add does not exist. 78 | 79 | ## Multilingual 80 | 81 | This action could work for any programming "language". 82 | 83 | Ideally you could write as "\`\`\`lang:external/file/path.py\`\`\`", but actually this action does not inspect "lang", just read path and copy&paste strings into target code block in markdown. 84 | 85 | So, missing "lang" is available: 86 | 87 | ```:src/helloworld.sh 88 | #!/bin/bash 89 | 90 | echo "hello" 91 | ``` 92 | 93 | ## Formatting 94 | 95 | Notice that this action uses markdown parser/formatter, so, if you don't update any code, readme may be updated if formatter works. 96 | --------------------------------------------------------------------------------