Compare commits

..

70 commits

Author SHA1 Message Date
Erik Flodin 7eabaee84c
Release 3.5.0
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
* Silence warnings when collecting alt files (#521)
  * Adjust handling of encrypt patterns to match 3.3.0 and older
  * Make encrypt exclude patterns only match encrypted files
  * Automatically exclude alt and template files (#234)
  * Support negative alt conditions (#365)
  * Handle filenames with space in bash completion (#341)
  * Add new yadm.filename template variable (#520)
2025-03-04 00:05:35 +01:00
Erik Flodin 4f4c5e2914
Update CHANGES and prepare for 3.5.0 2025-03-04 00:03:44 +01:00
Erik Flodin 0e0172769d
Add yadm.filename variable to default template processor
Some checks are pending
Tests / Tests (macos-13) (push) Waiting to run
Tests / Tests (macos-15) (push) Waiting to run
Tests / Tests (ubuntu-20.04) (push) Waiting to run
Tests / Tests (ubuntu-24.04) (push) Waiting to run
Tests / Tests (windows-2022) (push) Waiting to run
Similar to yadm.source but reflects the current file also in included
files (#520).
2025-03-03 23:47:16 +01:00
Erik Flodin 2ac90b004c
Fix handling of filenames with space in bash completion (#341)
By calling __gitcompappend with the same suffix as the completion for git
does (i.e. " ") we can remove the call to sort -u as it is no longer
needed (bash seems to remove duplicates) and then spaces are handled correctly.

Also make the completion available in testhost.
2025-03-03 21:36:36 +01:00
Erik Flodin bee1558a4e
Minor cleanups of alt handling
Also correct alt conditions precedence list in manual.
2025-03-02 22:18:09 +01:00
AaronYoung5 9ff5e09650
Add support for negative alt conditions (#522) 2025-03-02 22:06:12 +01:00
Erik Flodin d4796108f4
Automatically exclude alt links and template files
unless yadm.auto-exclude is set to false (#234, #465).

Alt files exclude pattern will be written to $GIT_DIR/info/exclude.yadm-alt and
encrypt files exclude patthern to ...yadm-encrypt. Then these two files will be
merged together and added to $GIT_DIR/info/exclude whenever one of them has
changed.
2025-03-02 21:05:43 +01:00
Erik Flodin 6726730701
parse_encrypt: Don't let e.g. "*.ext" match files in subdirs
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
This matches the behavior before 3.4.0.

Silent errors from ls-files to avoid warnings about e.g. directories that
aren't readable and also list files that would have been encrypted had they not
been tracked in git (#521).

Fix the patterns written to info/exclude so that they match the same files as
are encrypted (e.g. *.key should only match .key files in the topdir, not in
subdirs).
2025-02-25 23:04:23 +01:00
Erik Flodin 5648f8b337
Release 3.4.0
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
* Improve and harden alt file regeneration (#466)
* Fix "yadm config" in fish completion (#491)
* Fix "yadm clone" when not run in "$YADM_WORK" (#513)
* Output the actual paths in help message (#376)
* Verify all alt conditions for templates (#478)
* Ignore case in alt and default template conditions (#455, #456)
* Fall back to ID for distro family if ID_LIKE is not available (#494)
* Support overriding distro and distro family (#430)
* Improve support for Bash 3 (the default version on macOS)
* Make "yadm clone --recursive" work as expected (#517)
* Don't include files multiple times in archive (#125)
* Document YADM_HOOK_DATA and YADM_HOOK_DIR env variables (#343)
* Support alt dirs with deeply nested tracked files (#495)
2025-02-09 22:28:48 +01:00
Erik Flodin c90aa86051
Update CHANGES and prepare for 3.4.0 2025-02-09 22:24:18 +01:00
Erik Flodin 7e19d21f09
Document YADM_HOOK_DATA and YADM_HOOK_DIR env variables (#343)
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
2025-01-27 22:25:03 +01:00
Erik Flodin 1505b7ec8f
Add test to verify that file is only included once in archive
As of the switch (in 4511f5d9) to use git ls-files to list files to include in
archive, duplicate matches are automatically removed (fixes #125).
2025-01-27 21:54:10 +01:00
Erik Flodin 02b4019bc6
Add support and tests for clone --recurse-submodules
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
Including tests for clone --recursive.
2025-01-20 00:01:22 +01:00
Christof Warlich a86f2381b6
Make "yadm clone --recursive" work as expected (#517)
The --recursive switch was ignored when YADM clones a dotfile repository.
This commit causes "yadm clone --recursive" to also clone submodules
in one go, similar to what GIT does when given the --recursive switch.
2025-01-14 22:45:50 +01:00
Erik Flodin f33e7c2e1f
Run tests on github windows runner using WSL
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Tests / Tests (windows-2022) (push) Has been cancelled
2025-01-09 22:15:22 +01:00
Erik Flodin 0f5ee86b38
test: Make distroy family detection work as in yadm
So that family is properly set also on e.g. debian.
2025-01-09 22:08:50 +01:00
Christof Warlich 1e5612d707
Fix testing with WSL (#516) 2025-01-09 21:46:58 +01:00
Erik Flodin 1d69ce370c
Merge pull request #491
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
2024-12-15 17:25:27 +01:00
Erik Flodin 119d1ddbaa
Refactor template handling
Move common template logic out to a new template() function that calls one of
the existing template processors and then handles writing the result and
copying permissions.
2024-12-15 16:15:14 +01:00
Erik Flodin eb819782ee
Merge pull request #466 from rasa/improve-template-logic 2024-12-15 16:14:50 +01:00
Ross Smith II 7a4de1a247
Always remove temp_file on failure, other cleanup 2024-12-15 16:13:25 +01:00
Erik Flodin f5dfc7ab01
Format yadm and bootstrap with shfmt
Some checks failed
Tests / Tests (macos-13) (push) Has been cancelled
Tests / Tests (macos-15) (push) Has been cancelled
Tests / Tests (ubuntu-20.04) (push) Has been cancelled
Tests / Tests (ubuntu-24.04) (push) Has been cancelled
Command: shfmt -w -ln bash -i 2 -ci <file>
2024-12-11 20:07:38 +01:00
Erik Flodin bb21c9a267
Run tests directly on github runner instead of in the docker image
Some checks are pending
Tests / Tests (ubuntu-20.04) (push) Waiting to run
Tests / Tests (ubuntu-24.04) (push) Waiting to run
Tests / Tests (macos-13) (push) Waiting to run
Tests / Tests (macos-15) (push) Waiting to run
As this makes it possible to run the tests on different systems. Initially
ubuntu (20.04 and 24.04) and macOs (13 and 15).
2024-12-10 21:51:58 +01:00
Erik Flodin 4511f5d9c6
Use git ls-files to list files to encrypt
By using git ls-files instead of bash we can support ** also on macOS where the
included bash version (3) doesn't support globstar.
2024-12-10 21:51:58 +01:00
Erik Flodin 216d49ceef
Run mandoc or groff instead of man.REAL to check man page
Also fix all warnings reported by mandoc and apply some of the recommendations
from https://liw.fi/manpages/.
2024-12-10 21:29:27 +01:00
Erik Flodin 24772e7b4b
Fix test_upgrade when running outside of docker image
E.g. direct on github runner.
2024-12-10 21:28:51 +01:00
Erik Flodin 3093460d41
Ignore __pycache__/ 2024-12-10 21:28:51 +01:00
Erik Flodin c232894792
Make test_unit_configure_paths work with bash 3
Bash 3 doesn't understand declare -p so use regular parameter expansion instead
to print all variables that begin with YADM_ or GIT_.
2024-12-10 21:28:50 +01:00
Erik Flodin 8c2f833b43
Support overriding distro and distro family
Fixes #430.
2024-12-10 18:35:26 +01:00
Erik Flodin c092b7c099
Ignore case in alt and default template processor conditions
Some checks are pending
Tests / Tests (push) Waiting to run
This aligns all conditions with distro and distro_family.

Suggestion from #456.
2024-12-09 23:50:49 +01:00
Erik Flodin 6c1970fb41
Set distro family from ID line if no ID_LIKE line is found
See #456.
2024-12-09 23:13:43 +01:00
Erik Flodin 18d5f66542
Ignore case for yadm.distro and .distro_family in default template
Same as b2b0b14 but for if statements in default template processor.
2024-12-09 22:54:41 +01:00
Erik Flodin b2b0b143d6
Refactor alt handling
Some checks failed
Tests / Tests (push) Has been cancelled
* Simplify score_file() by using case in instead of nested ifs with regexps.
* Merge record_score() and record_template().
* Alt condition processing no longer stops when a template condition is seen
  but continues processing to verify that all conditions are valid (as the
  documentation says it should). Fixes #478.
* Support alt dirs with deeply nested tracked files (fixes #490).
* Use git ls-files to filter out which tracked files to consider for alt
  processing. Should speed up auto-alt (#505).
* Use nocasematch when comparing distro and distro_family. Fixed #455.
2024-12-06 23:10:52 +01:00
Erik Flodin b164d03594
Make relative_path match full dir and not just a prefix
Before this change, relative_path "/A/B/C" "/A/B/CD" would return "" instead of
the correct "../CD".
2024-12-06 23:10:49 +01:00
Erik Flodin 0b91140ea8
Output the actual paths in help message
Some checks are pending
Tests / Tests (push) Waiting to run
Fixes #376.
2024-12-06 00:07:45 +01:00
Erik Flodin 6ee9b472d1
Merge pull request #507 from AVM-Martin/fix/reset-yadm-work-index
Some checks failed
Tests / Tests (push) Has been cancelled
fix(clone): reset index of YADM_WORK
2024-11-29 23:10:35 +01:00
AVM.Martin ae3a149449
style: use pathspec for consistency 2024-11-26 19:22:18 +07:00
AVM.Martin 85e8c1ddfc
docs: fix comment 2024-11-26 19:21:18 +07:00
AVM.Martin a7939bec7b
style: join arguments 2024-11-26 19:20:59 +07:00
Erik Flodin ec10041024
Call bootstrap scripts with a tty
Some checks failed
Tests / Tests (push) Has been cancelled
Inspired by #449 but using read instead of mapfile to make it work with bash
3. Fixes #344.
2024-11-24 20:18:22 +01:00
Erik Flodin 3a1b236147
Link features in README.md to yadm.io (as suggested in #346)
Some checks are pending
Tests / Tests (push) Waiting to run
2024-11-24 17:03:56 +01:00
Erik Flodin 30fa6f08a4
Update testbed docker image
Some checks failed
Tests / Tests (push) Has been cancelled
* Update base image to Ubuntu 24.10. This uses a python version where j2cli no
  longer works when installed using pip so use the version from Ubuntu instead
  which has been patched to work.

* Update shellcheck, pylint, pytest, isort, flake8, black and yamllint to the
  latest versions. This closes #502.

* Use a longer expect timeout to fix tests failing when gpg is killed due to
  this timeout.

* Explicitly flush gpg-agent's cached passwords to fix failing tests with
  latest gnupg. Also clean up after tests to avoid having gpg-agents running
  after the test (e.g. when running tests directly without docker).
2024-11-11 22:30:41 +01:00
Erik Flodin 640b324401
Prepare for version 3.3.0
Some checks failed
Tests / Tests (push) Has been cancelled
* Support nested ifs in default template (#436)
 * Support include and ifs in default template includes (#406)
 * Support environment variables in ifs in default template (#488)
 * Support != in default template (#358, #477)
 * Fix multiple classes in default template on macOS (#437)
2024-11-08 20:23:23 +01:00
Erik Flodin 8e5d4b1578
Pass classes as separate arguments to template_default
Some checks are pending
Tests / Tests (push) Waiting to run
To work around problem with passing newlines in variable with awk on
darwin. This fixes #437.
2024-11-08 19:54:14 +01:00
Erik Flodin d74a41b1b4
Merge pull request #497 from erijo/template-default
Some checks failed
Tests / Tests (push) Has been cancelled
Rewrite default template to handle nested ifs, != and env vars in if
2024-11-06 22:25:40 +01:00
Erik Flodin 144f35b82d
Update links under .github/ to point to new github org
Some checks failed
Tests / Tests (push) Has been cancelled
2024-11-04 23:12:28 +01:00
Erik Flodin 8ba9823407
Rewrite default template to handle nested ifs, != and env vars in if
The awk script now performs all processing in the BEGIN block using an
implementation that is capable of handling if statements which contain nested
if statments (fixes #436). To make nested ifs look better, if, else and endif
lines can now have optional whitespace before {%.

Includes are now handled in the same way as the main file which means that
included files can both include other files and have if statements in addition
to variables (fixes #406). Include lines can now also have optional whitespace
before {%.

All variables are handled in the same way now so it's now possible to use env
variables in if statements (fixes #488).

Also add support for != in addition to == (fixes #358). Thus it's now
e.g. possible to check if a variable is set (#477) by doing:

{% if yadm.class != ""%}
Class is set to {{ yadm.class }}
{% endif %}

A non-existing yadm or env variable is now replaced with the empty string.
2024-11-04 22:30:23 +01:00
Tim Byrne 6d3b9dac71
Merge branch 'move'
Some checks failed
Tests / Tests (push) Has been cancelled
2024-10-31 15:41:38 -05:00
Tim Byrne e097d16d88
Update docker image used by scheduled test 2024-10-31 15:37:40 -05:00
Tim Byrne 3ecbe7ec27
Merge branch 'move' (Updating location)
Some checks are pending
Tests / Tests (push) Waiting to run
2024-10-30 17:55:49 -05:00
Tim Byrne 38880abc68
Update repo location 2024-10-30 17:18:49 -05:00
AVM.Martin aba434274e
fix(clone): reset index of YADM_WORK 2024-07-21 07:02:49 +07:00
AVM.Martin 12c51e130b
test(clone): run inside YADM_WORK sub-directory
Whenever using `clone` command inside YADM_WORK sub-directory, we need
to checkout the correct repo contents.

Steps to reproduce:

```bash
mkdir $HOME/subdir
cd $HOME/subdir

yadm clone --bootstrap "<repo-url>"
yadm status
```
2024-07-21 07:02:49 +07:00
heddxh 16deac67b5
fix: yadm config in fish completion 2024-07-16 17:25:44 +08:00
Tim Byrne 0a5e7aa353
Merge pull request #470 from LFd3v/master 2023-11-12 07:33:09 -06:00
LFdev 040dd461bd
Update Arch Linux badge in README.md
community repo was merged with extra, please refer to:

https://archlinux.org/news/git-migration-announcement/
https://archlinux.org/news/git-migration-completed/
2023-11-08 23:02:14 +00:00
Ross Smith II 95d7bae7b3
Improve and harden alt file regeneration
Improvements include:

1. Skip writing a temporary file if the file contents are unchanged
2. Better error reporting if templating program fails
3. Better error reporting/handling if file creation, mv, or chmod fail
4. Quiet logs by not outputing "Creating output..." line twice (debug & loud)
2023-10-11 14:58:27 -07:00
Tim Byrne 76ce3defea
Changes for black compliance 2023-07-12 09:20:00 -05:00
Tim Byrne e704175201
Add black to linting 2023-07-12 09:20:00 -05:00
Tim Byrne 1b36bf2bb6
Move all test/linting configs into pyproject.toml
Except for flake8 which doesn't support pyproject.toml yet.
2023-07-12 09:19:59 -05:00
Tim Byrne 7af2af52ae
Lint using isort 2023-07-12 09:19:59 -05:00
Tim Byrne bbc1ca17f0
Changes for new flake8 compliance 2023-07-12 09:19:59 -05:00
Tim Byrne 8a87f4a30f
Changes for new pylint compliance 2023-07-12 09:19:59 -05:00
Tim Byrne f9e0368385
Changes for new shellcheck compliance 2023-07-12 09:19:59 -05:00
Tim Byrne c8acf77e04
Adjust pinentry mock
The new test Docker image has a newer gnupg which does not behave the
same way, handling invalid passwords. This type of error is simulated
using an ICP error in the pinentry protocol.
2023-07-12 09:19:59 -05:00
Tim Byrne 9752d801ae
Replace deprecated "pipes" with "shlex" 2023-07-12 09:19:58 -05:00
Tim Byrne 84437894b5
Use man.REAL 2023-07-12 09:19:58 -05:00
Tim Byrne 82ba16db34
Support testing with newer version of Git 2023-07-12 09:19:58 -05:00
Tim Byrne 2253e52ab7
Update test Docker image
* Update base image (which uses a newer python)
* Update linters
* Update other dependencies
2023-07-12 09:19:53 -05:00
Tim Byrne e4bb8a79a4
Update to shields.io new API 2023-01-23 13:51:06 -06:00
80 changed files with 3809 additions and 3317 deletions

2
.flake8 Normal file
View file

@ -0,0 +1,2 @@
[flake8]
max-line-length = 120

View file

@ -1,7 +1,7 @@
# Introduction
Thank you for considering contributing to **yadm**. I develop this project in my
limited spare time, so help is very appreciated.
Thank you for considering contributing to **yadm**. We develop this project in
our limited spare time, so help is very appreciated.
All contributors must follow our [Code of Conduct][conduct]. Please make sure
you are welcoming and friendly during your interactions, and report any
@ -17,7 +17,8 @@ To contribute, you can:
* Star the yadm repo, the star count helps others discover yadm.
* Report [bugs](#reporting-a-bug)
* Request [features/enhancements](#suggesting-a-feature-or-enhancement)
* Contribute changes to [code, tests](#contributing-code), and [documentation](#improving-documentation)
* Contribute changes to [code, tests](#contributing-code), and
[documentation](#improving-documentation)
* Maintain installation [packages](#maintaining-packages)
* Help other users by [answering support questions](#answering-support-questions)
@ -70,14 +71,14 @@ Consider trying to reproduce the bug inside a docker container using the
[yadm/testbed][] docker image. Doing so will greatly increase the likelihood of
the problem being fixed.
The easiest way to start this container, is to clone the [TheLocehiliosan/yadm
The easiest way to start this container, is to clone the [yadm
repo][yadm-repo], and use the `scripthost` make target. _(You will need `make`
and `docker` installed.)_
For example:
```text
$ git clone https://github.com/TheLocehiliosan/yadm.git
$ git clone https://github.com/yadm-dev/yadm.git
$ cd yadm
$ make scripthost version=1.12.0
Starting scripthost version="1.12.0" (recording script)
@ -203,7 +204,7 @@ these principles when making changes.
3. Add the official repository (`upstream`) as a remote repository.
```text
$ git remote add upstream https://github.com/TheLocehiliosan/yadm.git
$ git remote add upstream https://github.com/yadm-dev/yadm.git
```
4. Verify you can run the test harness. _(This will require dependencies:
@ -361,25 +362,24 @@ see if you can help.
[attach-help]: https://help.github.com/en/articles/file-attachments-on-issues-and-pull-requests
[commit-style]: https://chris.beams.io/posts/git-commit/#seven-rules
[conduct]: CODE_OF_CONDUCT.md
[contrib-hooks]: https://github.com/TheLocehiliosan/yadm/tree/master/contrib/hooks
[contrib-hooks]: https://github.com/yadm-dev/yadm/tree/master/contrib/hooks
[flake8]: https://pypi.org/project/flake8/
[groff-man]: https://www.gnu.org/software/groff/manual/html_node/man.html
[hooks-help]: https://github.com/TheLocehiliosan/yadm/blob/master/yadm.md#hooks
[hooks-help]: https://github.com/yadm-dev/yadm/blob/master/yadm.md#hooks
[html-proofer]: https://github.com/gjtorikian/html-proofer
[jekyll]: https://jekyllrb.com
[new-bug]: https://github.com/TheLocehiliosan/yadm/issues/new?template=BUG_REPORT.md
[new-feature]: https://github.com/TheLocehiliosan/yadm/issues/new?template=FEATURE_REQUEST.md
[open-issues]: https://github.com/TheLocehiliosan/yadm/issues
[new-bug]: https://github.com/yadm-dev/yadm/issues/new?template=BUG_REPORT.md
[new-feature]: https://github.com/yadm-dev/yadm/issues/new?template=FEATURE_REQUEST.md
[open-issues]: https://github.com/yadm-dev/yadm/issues
[pr-help]: https://help.github.com/en/articles/creating-a-pull-request-from-a-fork
[pylint]: https://pylint.org/
[pytest]: https://pytest.org/
[questions]: https://github.com/TheLocehiliosan/yadm/labels/question
[refactor]: https://github.com/TheLocehiliosan/yadm/issues/146
[questions]: https://github.com/yadm-dev/yadm/labels/question
[shellcheck]: https://www.shellcheck.net
[signing-commits]: https://help.github.com/en/articles/signing-commits
[tpope-style]: https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
[yadm-man]: https://github.com/TheLocehiliosan/yadm/blob/master/yadm.md
[yadm-repo]: https://github.com/TheLocehiliosan/yadm
[yadm-man]: https://github.com/yadm-dev/yadm/blob/master/yadm.md
[yadm-repo]: https://github.com/yadm-dev/yadm
[yadm/jekyll]: https://hub.docker.com/r/yadm/jekyll
[yadm/testbed]: https://hub.docker.com/r/yadm/testbed
[yamllint]: https://github.com/adrienverge/yamllint

View file

@ -1,6 +1,6 @@
<!--
Before submitting, please search open and closed issues at
https://github.com/TheLocehiliosan/yadm/issues to avoid duplication.
https://github.com/yadm-dev/yadm/issues to avoid duplication.
If you have found a security vulnerability, do NOT open an issue.
Email yadm@yadm.io instead.

View file

@ -8,7 +8,7 @@ assignees: ''
---
<!--
Before submitting, please search open and closed issues at
https://github.com/TheLocehiliosan/yadm/issues to avoid duplication.
https://github.com/yadm-dev/yadm/issues to avoid duplication.
If you have found a security vulnerability, do NOT open an issue.
Email yadm@yadm.io instead.
@ -25,10 +25,10 @@ Can this be reproduced with the yadm/testbed docker image: [Yes/No]
Consider trying to reproduce the bug inside a docker container using the
yadm/testbed docker image. https://hub.docker.com/r/yadm/testbed
The easiest way to start this container, is to clone the TheLocehiliosan/yadm
The easiest way to start this container, is to clone the yadm-dev/yadm
repo, and use the "scripthost" make target. For example:
$ git clone https://github.com/TheLocehiliosan/yadm.git
$ git clone https://github.com/yadm-dev/yadm.git
$ cd yadm
$ make scripthost version=1.11.0
Starting scripthost version="1.11.0" (recording script)

View file

@ -8,7 +8,7 @@ assignees: ''
---
<!--
Before submitting, please search open and closed issues at
https://github.com/TheLocehiliosan/yadm/issues to avoid duplication.
https://github.com/yadm-dev/yadm/issues to avoid duplication.
-->
### Is your feature request related to a problem? Please describe.

View file

@ -8,7 +8,7 @@ assignees: ''
---
<!--
Before submitting, please search open and closed issues at
https://github.com/TheLocehiliosan/yadm/issues to avoid duplication.
https://github.com/yadm-dev/yadm/issues to avoid duplication.
-->
### This issue is about

View file

@ -8,11 +8,11 @@ assignees: ''
---
<!--
Before submitting, please search open and closed issues at
https://github.com/TheLocehiliosan/yadm/issues to avoid duplication.
https://github.com/yadm-dev/yadm/issues to avoid duplication.
Please also consult:
* The FAQ: https://yadm.io/docs/faq
* The manual: https://github.com/TheLocehiliosan/yadm/blob/master/yadm.md
* The manual: https://github.com/yadm-dev/yadm/blob/master/yadm.md
-->
### This question is about

View file

@ -29,6 +29,6 @@ Be sure to preface the issue/PR numbers with a "#".
Please review [yadm's Contributing Guide][3] for best practices.
[1]: https://github.com/TheLocehiliosan/yadm/blob/master/.github/CONTRIBUTING.md#test-conventions
[1]: https://github.com/yadm-dev/yadm/blob/master/.github/CONTRIBUTING.md#test-conventions
[2]: https://help.github.com/en/articles/signing-commits
[3]: https://github.com/TheLocehiliosan/yadm/blob/master/.github/CONTRIBUTING.md
[3]: https://github.com/yadm-dev/yadm/blob/master/.github/CONTRIBUTING.md

View file

@ -14,7 +14,7 @@ jobs:
docker create -t
--name yadm-website
--entrypoint test/validate
yadm/jekyll:2019-10-17;
yadm/jekyll:2024-10-31;
docker cp ./ yadm-website:/srv/jekyll
- name: Test Site
run: docker start yadm-website -a

View file

@ -1,13 +1,129 @@
---
name: Tests
on: # yamllint disable-line rule:truthy
- push
- pull_request
- workflow_dispatch
env:
SC_VER: "0.10.0"
ESH_VER: "0.3.2"
jobs:
Tests:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-24.04
- macos-13
- macos-15
- windows-2022
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v2
- name: Tests
run: make test
- uses: actions/checkout@v4
- uses: Vampire/setup-wsl@v4
if: ${{ runner.os == 'Windows' }}
- name: Install dependencies on Linux
if: ${{ runner.os == 'Linux' }}
run: |
sudo apt-get update
sudo apt-get install -y \
expect \
${{ matrix.os != 'ubuntu-20.04' && 'j2cli' || '' }}
- name: Install dependencies on macOS
if: ${{ runner.os == 'macOS' }}
run: |
command -v expect || brew install expect
- name: Install dependencies on Windows (WSL)
if: ${{ runner.os == 'Windows' }}
shell: wsl-bash {0}
run: |
apt-get update
apt-get install -y --no-install-recommends \
dos2unix \
expect \
gettext-base \
git \
gnupg \
j2cli \
lsb-release \
man \
python3-pip
- name: Prepare tools directory
run: |
mkdir "${{ runner.temp }}/tools"
echo "${{ runner.temp }}/tools" >> "${{ github.path }}"
- name: Install shellcheck
run: |
cd "${{ runner.temp }}"
OS=${{ runner.os == 'macOS' && 'darwin' || 'linux' }}
ARCH=${{ runner.arch == 'ARM64' && 'aarch64' || 'x86_64' }}
BASE_URL="https://github.com/koalaman/shellcheck/releases/download"
SC="v$SC_VER/shellcheck-v$SC_VER.$OS.$ARCH.tar.xz"
curl -L "$BASE_URL/$SC" | tar Jx shellcheck-v$SC_VER/shellcheck
mv shellcheck-v$SC_VER/shellcheck tools
- name: Install esh
run: |
cd "${{ runner.temp }}/tools"
BASE_URL="https://github.com/jirutka/esh/raw/refs/tags"
curl -L -o esh "$BASE_URL/v$ESH_VER/esh"
chmod +x esh
- name: Add old yadm versions # to test upgrades
run: |
for version in 1.12.0 2.5.0; do
git fetch origin $version:refs/tags/$version
git cat-file blob $version:yadm \
> "${{ runner.temp }}/tools/yadm-$version"
chmod +x "${{ runner.temp }}/tools/yadm-$version"
done
- name: Set up Python 3.11
if: ${{ runner.os == 'macOS' || matrix.os == 'ubuntu-20.04' }}
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install dependencies and run tests (Linux/macOS)
if: ${{ runner.os != 'Windows' }}
run: |
git config --global user.email test@yadm.io
git config --global user.name "Yadm Test"
python3 -m pip install --upgrade pip
python3 -m pip install -r test/requirements.txt
pytest -v --color=yes --basetemp="${{ runner.temp }}/pytest"
- name: Install dependencies and run tests (WSL)
if: ${{ runner.os == 'Windows' }}
shell: wsl-bash {0}
run: |
git config --global user.email test@yadm.io
git config --global user.name "Yadm Test"
git config --global protocol.file.allow always
dos2unix yadm.1 .github/workflows/*.yml test/pinentry-mock
chmod +x test/pinentry-mock
python3 -m pip install --upgrade pip
python3 -m pip install -r test/requirements.txt
pytest -v --color=yes

1
.gitignore vendored
View file

@ -5,3 +5,4 @@
.testyadm
_site
testenv
__pycache__/

31
CHANGES
View file

@ -1,3 +1,34 @@
3.5.0
* Silence warnings when collecting alt files (#521)
* Adjust handling of encrypt patterns to match 3.3.0 and older
* Make encrypt exclude patterns only match encrypted files
* Automatically exclude alt and template files (#234)
* Support negative alt conditions (#365)
* Handle filenames with space in bash completion (#341)
* Add new yadm.filename template variable (#520)
3.4.0
* Improve and harden alt file regeneration (#466)
* Fix "yadm config" in fish completion (#491)
* Fix "yadm clone" when not run in "$YADM_WORK" (#513)
* Output the actual paths in help message (#376)
* Verify all alt conditions for templates (#478)
* Ignore case in alt and default template conditions (#455, #456)
* Fall back to ID for distro family if ID_LIKE is not available (#494)
* Support overriding distro and distro family (#430)
* Improve support for Bash 3 (the default version on macOS)
* Make "yadm clone --recursive" work as expected (#517)
* Don't include files multiple times in archive (#125)
* Document YADM_HOOK_DATA and YADM_HOOK_DIR env variables (#343)
* Support alt dirs with deeply nested tracked files (#495)
3.3.0
* Support nested ifs in default template (#436)
* Support include and ifs in default template includes (#406)
* Support environment variables in ifs in default template (#488)
* Support != in default template (#358, #477)
* Fix multiple classes in default template on macOS (#437)
3.2.2
* Support spaces in distro/distro-family (#432)
* Fix zsh hanging when tab completing add/checkout (#417)

View file

@ -3,31 +3,34 @@ CONTRIBUTORS
Tim Byrne
Erik Flodin
Martin Zuther
Jan Schulz
Ross Smith II
Jan Schulz
Jonathan Daigle
Luis López
Tin Lai
Espen Henriksen
AaronYoung5
Cameron Eagans
Klas Mellbourn
James Clark
Glenn Waters
Nicolas signed-log FORMICHELLA
Tomas Cernaj
AVM.Martin
Joshua Cold
jonasc
Nicolas stig124 FORMICHELLA
Chad Wade Day, Jr
Sébastien Gross
Christof Warlich
David Mandelberg
Paulo Köch
Oren Zipori
Daniel Gray
Paraplegic Racehorse
japm48
Siôn Le Roux
Mateusz Piotrowski
japm48
Uroš Golja
Satoshi Ohki
Jonas
@ -35,18 +38,21 @@ Franciszek Madej
Daniel Wagenknecht
Stig Palmquist
Patrick Hof
con-f-use
Samisafool
LFdev
con-f-use
Bram Ceulemans
Travis A. Everett
Sheng Yang
Jared Smartt
Adam Jimerson
dessert1
addshore
Tim Condit
Thomas Luzat
Russ Allbery
Patrick Roddy
heddxh
dessert1
Brayden Banks
Alexandre GV
addshore
Felipe S. S. Schneider

View file

@ -1,5 +1,5 @@
PYTESTS = $(wildcard test/test_*.py)
IMAGE = docker.io/yadm/testbed:2022-01-07
IMAGE = docker.io/yadm/testbed:2024-11-11
OCI = docker
.PHONY: all
@ -123,6 +123,7 @@ testhost: require-docker .testyadm
--hostname testhost \
--rm -it \
-v "$(CURDIR)/.testyadm:/bin/yadm:ro" \
-v "$(CURDIR)/completion/bash/yadm:/usr/share/bash-completion/completions/yadm:ro" \
$(IMAGE) \
bash -l
@ -176,7 +177,7 @@ man-ps:
@groff -man -Tps ./yadm.1 > yadm.ps
yadm.md: yadm.1
@groff -man -Tutf8 -Z ./yadm.1 | grotty -c | col -bx | sed 's/^[A-Z]/## &/g' | sed '/yadm(1)/d' > yadm.md
@groff -man -Tutf8 -Z ./yadm.1 | grotty -c | col -bx | sed 's/^[A-Z]/## &/g' | sed '/YADM(1)/d' > yadm.md
.PHONY: contrib
contrib: SHELL = /bin/bash

View file

@ -18,11 +18,12 @@
**yadm** is a tool for managing [dotfiles][].
* Based on [Git][], with full range of Git's features
* Supports system-specific alternative files or templated files
* Encryption of private data using [GnuPG][], [OpenSSL][], [transcrypt][], or
[git-crypt][]
* Customizable initialization (bootstrapping)
* Customizable hooks for before and after any operation
* Supports system-specific [alternative][feature-alternates] files or
[templated][feature-templates] files
* [Encryption][feature-encryption] of private data using [GnuPG][],
[OpenSSL][], [transcrypt][], or [git-crypt][]
* Customizable initialization ([bootstrapping][feature-bootstrap])
* Customizable [hooks][feature-hooks] for before and after any operation
Complete features, usage, examples and installation instructions can be found on
the [yadm.io][website-link] website.
@ -56,32 +57,36 @@ The star count helps others discover yadm.
[Git]: https://git-scm.com/
[GnuPG]: https://gnupg.org/
[OpenSSL]: https://www.openssl.org/
[arch-badge]: https://img.shields.io/archlinux/v/community/any/yadm
[arch-link]: https://archlinux.org/packages/community/any/yadm/
[dev-pages-badge]: https://img.shields.io/github/workflow/status/TheLocehiliosan/yadm/Test%20Site/dev-pages?label=dev-pages
[develop-badge]: https://img.shields.io/github/workflow/status/TheLocehiliosan/yadm/Tests/develop?label=develop
[develop-commits]: https://github.com/TheLocehiliosan/yadm/commits/develop
[develop-date]: https://img.shields.io/github/last-commit/TheLocehiliosan/yadm/develop.svg?label=develop
[arch-badge]: https://img.shields.io/archlinux/v/extra/any/yadm
[arch-link]: https://archlinux.org/packages/extra/any/yadm/
[dev-pages-badge]: https://img.shields.io/github/actions/workflow/status/yadm-dev/yadm/test.yml?branch=dev-pages
[develop-badge]: https://img.shields.io/github/actions/workflow/status/yadm-dev/yadm/test.yml?branch=develop
[develop-commits]: https://github.com/yadm-dev/yadm/commits/develop
[develop-date]: https://img.shields.io/github/last-commit/yadm-dev/yadm/develop.svg?label=develop
[dotfiles]: https://en.wikipedia.org/wiki/Hidden_file_and_hidden_directory
[gh-pages-badge]: https://img.shields.io/github/workflow/status/TheLocehiliosan/yadm/Test%20Site/gh-pages?label=gh-pages
[feature-alternates]: https://yadm.io/docs/alternates
[feature-bootstrap]: https://yadm.io/docs/bootstrap
[feature-hooks]: https://yadm.io/docs/hooks
[feature-encryption]: https://yadm.io/docs/encryption
[feature-templates]: https://yadm.io/docs/templates
[gh-pages-badge]: https://img.shields.io/github/actions/workflow/status/yadm-dev/yadm/test.yml?branch=gh-pages
[git-crypt]: https://github.com/AGWA/git-crypt
[homebrew-badge]: https://img.shields.io/homebrew/v/yadm.svg
[homebrew-link]: https://formulae.brew.sh/formula/yadm
[license-badge]: https://img.shields.io/github/license/TheLocehiliosan/yadm.svg
[license-link]: https://github.com/TheLocehiliosan/yadm/blob/master/LICENSE
[master-badge]: https://img.shields.io/github/workflow/status/TheLocehiliosan/yadm/Tests/master?label=master
[master-commits]: https://github.com/TheLocehiliosan/yadm/commits/master
[master-date]: https://img.shields.io/github/last-commit/TheLocehiliosan/yadm/master.svg?label=master
[obs-badge]: https://img.shields.io/badge/OBS-v3.2.2-blue
[obs-link]: https://software.opensuse.org//download.html?project=home%3ATheLocehiliosan%3Ayadm&package=yadm
[releases-badge]: https://img.shields.io/github/tag/TheLocehiliosan/yadm.svg?label=latest+release
[releases-link]: https://github.com/TheLocehiliosan/yadm/releases
[license-badge]: https://img.shields.io/github/license/yadm-dev/yadm.svg
[license-link]: https://github.com/yadm-dev/yadm/blob/master/LICENSE
[master-badge]: https://img.shields.io/github/actions/workflow/status/yadm-dev/yadm/test.yml?branch=master
[master-commits]: https://github.com/yadm-dev/yadm/commits/master
[master-date]: https://img.shields.io/github/last-commit/yadm-dev/yadm/master.svg?label=master
[obs-badge]: https://img.shields.io/badge/OBS-v3.5.0-blue
[obs-link]: https://software.opensuse.org/download.html?project=home%3ATheLocehiliosan%3Ayadm&package=yadm
[releases-badge]: https://img.shields.io/github/tag/yadm-dev/yadm.svg?label=latest+release
[releases-link]: https://github.com/yadm-dev/yadm/releases
[transcrypt]: https://github.com/elasticdog/transcrypt
[travis-ci]: https://travis-ci.com/TheLocehiliosan/yadm/branches
[website-commits]: https://github.com/TheLocehiliosan/yadm/commits/gh-pages
[website-date]: https://img.shields.io/github/last-commit/TheLocehiliosan/yadm/gh-pages.svg?label=website
[website-commits]: https://github.com/yadm-dev/yadm/commits/gh-pages
[website-date]: https://img.shields.io/github/last-commit/yadm-dev/yadm/gh-pages.svg?label=website
[website-link]: https://yadm.io/
[workflow-dev-pages]: https://github.com/thelocehiliosan/yadm/actions?query=workflow%3a%22test+site%22+branch%3adev-pages
[workflow-develop]: https://github.com/TheLocehiliosan/yadm/actions?query=workflow%3ATests+branch%3Adevelop
[workflow-gh-pages]: https://github.com/thelocehiliosan/yadm/actions?query=workflow%3a%22test+site%22+branch%3agh-pages
[workflow-master]: https://github.com/TheLocehiliosan/yadm/actions?query=workflow%3ATests+branch%3Amaster
[workflow-dev-pages]: https://github.com/yadm-dev/yadm/actions?query=workflow%3a%22test+site%22+branch%3adev-pages
[workflow-develop]: https://github.com/yadm-dev/yadm/actions?query=workflow%3ATests+branch%3Adevelop
[workflow-gh-pages]: https://github.com/yadm-dev/yadm/actions?query=workflow%3a%22test+site%22+branch%3agh-pages
[workflow-master]: https://github.com/yadm-dev/yadm/actions?query=workflow%3ATests+branch%3Amaster

View file

@ -29,24 +29,26 @@
# source <(curl -L bootstrap.yadm.io)
#
YADM_REPO="https://github.com/TheLocehiliosan/yadm"
YADM_REPO="https://github.com/yadm-dev/yadm"
YADM_RELEASE=${release:-master}
REPO_URL=""
function _private_yadm() {
unset -f yadm
if command -v yadm &> /dev/null; then
if command -v yadm &>/dev/null; then
echo "Found yadm installed locally, removing remote yadm() function"
unset -f _private_yadm
command yadm "$@"
else
function yadm() { _private_yadm "$@"; }; export -f yadm
function yadm() { _private_yadm "$@"; }
export -f yadm
echo WARNING: Using yadm remotely. You should install yadm locally.
curl -fsSL "$YADM_REPO/raw/$YADM_RELEASE/yadm" | bash -s -- "$@"
fi
}
export -f _private_yadm
function yadm() { _private_yadm "$@"; }; export -f yadm
function yadm() { _private_yadm "$@"; }
export -f yadm
# if being sourced, return here, otherwise continue processing
return 2>/dev/null
@ -57,7 +59,7 @@ function remote_yadm() {
}
function ask_about_source() {
if ! command -v yadm &> /dev/null; then
if ! command -v yadm &>/dev/null; then
echo
echo "***************************************************"
echo "yadm is NOT currently installed."
@ -83,7 +85,7 @@ function build_url() {
echo " 3. GitLab"
echo " 4. Other"
echo
read -r -p "Where is your repo? (1/2/3/4) ->" choice < /dev/tty
read -r -p "Where is your repo? (1/2/3/4) ->" choice </dev/tty
case $choice in
1)
REPO_URL="https://github.com/"
@ -97,7 +99,7 @@ function build_url() {
*)
echo
echo Please specify the full URL of your dotfiles repo
read -r -p "URL ->" choice < /dev/tty
read -r -p "URL ->" choice </dev/tty
REPO_URL="$choice"
return
;;
@ -107,7 +109,7 @@ function build_url() {
echo "Provide your user and repo separated by '/'"
echo "For example: UserName/dotfiles"
echo
read -r -p "User/Repo ->" choice < /dev/tty
read -r -p "User/Repo ->" choice </dev/tty
[[ "$choice" =~ ^[^[:space:]]+/[^[:space:]]+$ ]] || {
echo "Not formatted as USER/REPO"
REPO_URL=

View file

@ -39,7 +39,7 @@ Load `_yadm` as a plugin in your `.zshrc`:
```zsh
fpath=("$ZPLUG_HOME/bin" $fpath)
zplug "TheLocehiliosan/yadm", use:"completion/zsh/_yadm", as:command, defer:2
zplug "yadm-dev/yadm", use:"completion/zsh/_yadm", as:command, defer:2
```
## Fish (manual installation)

View file

@ -1,88 +1,85 @@
# test if git completion is missing, but loader exists, attempt to load
if ! declare -F _git > /dev/null && ! declare -F __git_wrap__git_main > /dev/null; then
if declare -F _completion_loader > /dev/null; then
if ! declare -F _git >/dev/null && ! declare -F __git_wrap__git_main >/dev/null; then
if declare -F _completion_loader >/dev/null; then
_completion_loader git
fi
fi
# only operate if git completion is present
if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; then
if declare -F _git >/dev/null || declare -F __git_wrap__git_main >/dev/null; then
_yadm() {
local current=${COMP_WORDS[COMP_CWORD]}
local penultimate
if [ "$((COMP_CWORD-1))" -ge "0" ]; then
penultimate=${COMP_WORDS[COMP_CWORD-1]}
if ((COMP_CWORD >= 1)); then
penultimate=${COMP_WORDS[COMP_CWORD - 1]}
fi
local antepenultimate
if [ "$((COMP_CWORD-2))" -ge "0" ]; then
antepenultimate=${COMP_WORDS[COMP_CWORD-2]}
if ((COMP_CWORD >= 2)); then
antepenultimate=${COMP_WORDS[COMP_CWORD - 2]}
fi
local -x GIT_DIR
# shellcheck disable=SC2034
GIT_DIR="$(yadm introspect repo 2>/dev/null)"
case "$penultimate" in
bootstrap)
COMPREPLY=()
return 0
;;
;;
config)
COMPREPLY=( $(compgen -W "$(yadm introspect configs 2>/dev/null)") )
COMPREPLY=($(compgen -W "$(yadm introspect configs 2>/dev/null)"))
return 0
;;
;;
decrypt)
COMPREPLY=( $(compgen -W "-l" -- "$current") )
COMPREPLY=($(compgen -W "-l" -- "$current"))
return 0
;;
;;
init)
COMPREPLY=( $(compgen -W "-f -w" -- "$current") )
COMPREPLY=($(compgen -W "-f -w" -- "$current"))
return 0
;;
;;
introspect)
COMPREPLY=( $(compgen -W "commands configs repo switches" -- "$current") )
COMPREPLY=($(compgen -W "commands configs repo switches" -- "$current"))
return 0
;;
;;
help)
COMPREPLY=() # no specific help yet
return 0
;;
;;
list)
COMPREPLY=( $(compgen -W "-a" -- "$current") )
COMPREPLY=($(compgen -W "-a" -- "$current"))
return 0
;;
;;
esac
case "$antepenultimate" in
clone)
COMPREPLY=( $(compgen -W "-f -w -b --bootstrap --no-bootstrap" -- "$current") )
COMPREPLY=($(compgen -W "-f -w -b --bootstrap --no-bootstrap" -- "$current"))
return 0
;;
;;
esac
local yadm_switches=( $(yadm introspect switches 2>/dev/null) )
local yadm_switches=($(yadm introspect switches 2>/dev/null))
# this condition is so files are completed properly for --yadm-xxx options
if [[ " ${yadm_switches[*]} " != *" $penultimate "* ]]; then
# TODO: somehow solve the problem with [--yadm-xxx option] being
# incompatible with what git expects, namely [--arg=option]
if declare -F _git > /dev/null; then
if declare -F _git >/dev/null; then
_git
else
__git_wrap__git_main
fi
fi
if [[ "$current" =~ ^- ]]; then
local matching
matching=$(compgen -W "${yadm_switches[*]}" -- "$current")
__gitcompappend "$matching"
__gitcompappend "${yadm_switches[*]}" "" "$current" " "
fi
# Find the index of where the sub-command argument should go.
local command_idx
for (( command_idx=1 ; command_idx < ${#COMP_WORDS[@]} ; command_idx++ )); do
for ((command_idx = 1; command_idx < ${#COMP_WORDS[@]}; command_idx++)); do
local command_idx_arg="${COMP_WORDS[$command_idx]}"
if [[ " ${yadm_switches[*]} " = *" $command_idx_arg "* ]]; then
let command_idx++
@ -93,19 +90,11 @@ if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; t
fi
done
if [[ "$COMP_CWORD" = "$command_idx" ]]; then
local matching
matching=$(compgen -W "$(yadm introspect commands 2>/dev/null)" -- "$current")
__gitcompappend "$matching"
__gitcompappend "$(yadm introspect commands 2>/dev/null)" "" "$current" " "
fi
# remove duplicates found in COMPREPLY (a native bash way could be better)
if [ -n "${COMPREPLY[*]}" ]; then
COMPREPLY=($(echo "${COMPREPLY[@]}" | sort -u))
fi
}
complete -o bashdefault -o default -F _yadm yadm 2>/dev/null \
|| complete -o default -F _yadm yadm
complete -o bashdefault -o default -o nospace -F _yadm yadm 2>/dev/null ||
complete -o default -o nospace -F _yadm yadm
fi

View file

@ -60,7 +60,7 @@ complete -x -c yadm -n '__fish_yadm_using_command introspect' -a (printf -- '%s\
complete -x -c yadm -n '__fish_yadm_needs_command' -a 'gitconfig' -d 'Pass options to the git config command'
complete -x -c yadm -n '__fish_yadm_needs_command' -a 'config' -d 'Configure a setting'
for name in (yadm introspect configs)
complete -x -c yadm -n '__fish_yadm_using_command config' -a '$name' -d 'yadm config'
complete -x -c yadm -n '__fish_yadm_using_command config' -a $name -d 'yadm config'
end
# yadm universial options

View file

@ -14,11 +14,16 @@ if [[ ! -d "$BOOTSTRAP_D" ]]; then
exit 1
fi
find -L "$BOOTSTRAP_D" -type f | sort | while IFS= read -r bootstrap; do
if [[ -x "$bootstrap" && ! "$bootstrap" =~ "##" && ! "$bootstrap" =~ "~$" ]]; then
if ! "$bootstrap"; then
echo "Error: bootstrap '$bootstrap' failed" >&2
exit 1
fi
declare -a bootstraps
while IFS= read -r bootstrap; do
if [[ -x "$bootstrap" && ! "$bootstrap" =~ "##" && ! "$bootstrap" =~ ~$ ]]; then
bootstraps+=("$bootstrap")
fi
done < <(find -L "$BOOTSTRAP_D" -type f | sort)
for bootstrap in "${bootstraps[@]}"; do
if ! "$bootstrap"; then
echo "Error: bootstrap '$bootstrap' failed" >&2
exit 1
fi
done

View file

@ -11,4 +11,4 @@ this is a place to share it.
I recommend *careful review* of any code from here before using it. No
guarantees of code quality is assumed.
[hooks-help]: https://github.com/TheLocehiliosan/yadm/blob/master/yadm.md#hooks
[hooks-help]: https://github.com/yadm-dev/yadm/blob/master/yadm.md#hooks

View file

@ -1,17 +0,0 @@
[BASIC]
good-names=pytestmark
[DESIGN]
max-args=14
max-locals=28
max-attributes=8
max-statements=65
[SIMILARITIES]
min-similarity-lines=8
[MESSAGES CONTROL]
disable=redefined-outer-name
[TYPECHECK]
ignored-modules=py

32
pyproject.toml Normal file
View file

@ -0,0 +1,32 @@
[tool.pytest.ini_options]
cache_dir = "/tmp"
addopts = "-ra"
markers = [
"deprecated", # marks tests for deprecated features (deselect with '-m "not deprecated"')
]
[tool.pylint.design]
max-args = 14
max-positional-arguments = 10
max-locals = 28
max-attributes = 8
max-statements = 65
[tool.pylint.format]
max-line-length = 120
[tool.pylint."messages control"]
disable = [
"redefined-outer-name",
]
[tool.pylint.similarities]
ignore-imports = "yes"
min-similarity-lines = 8
[tool.black]
line-length = 120
[tool.isort]
line_length = 120
profile = "black"

View file

@ -1,5 +0,0 @@
[pytest]
cache_dir = /tmp
addopts = -ra
markers =
deprecated: marks tests for deprecated features (deselect with '-m "not deprecated"')

View file

@ -1,9 +1,8 @@
FROM ubuntu:18.04
MAINTAINER Tim Byrne <sultan@locehilios.com>
FROM ubuntu:24.10
# Shellcheck and esh versions
ARG SC_VER=0.8.0
ARG ESH_VER=0.3.1
ARG SC_VER=0.10.0
ARG ESH_VER=0.3.2
# Install prerequisites and configure UTF-8 locale
RUN \
@ -14,6 +13,7 @@ RUN \
expect \
git \
gnupg \
j2cli \
locales \
lsb-release \
make \
@ -39,10 +39,9 @@ RUN cd /opt \
&& rm -f shellcheck-v$SC_VER.linux.x86_64.tar.xz \
&& ln -s /opt/shellcheck-v$SC_VER/shellcheck /usr/local/bin
# Upgrade pip3 and install requirements
# Install requirements
COPY test/requirements.txt /tmp/requirements.txt
RUN python3 -m pip install --upgrade pip setuptools \
&& python3 -m pip install --upgrade -r /tmp/requirements.txt \
RUN python3 -m pip install --break-system-packages -r /tmp/requirements.txt \
&& rm -f /tmp/requirements
# Install esh
@ -57,8 +56,8 @@ RUN mkdir /yadm \
&& echo "\t@false" >> /yadm/Makefile
# Include released versions of yadm to test upgrades
ADD https://raw.githubusercontent.com/TheLocehiliosan/yadm/1.12.0/yadm /usr/local/bin/yadm-1.12.0
ADD https://raw.githubusercontent.com/TheLocehiliosan/yadm/2.5.0/yadm /usr/local/bin/yadm-2.5.0
ADD https://raw.githubusercontent.com/yadm-dev/yadm/1.12.0/yadm /usr/local/bin/yadm-1.12.0
ADD https://raw.githubusercontent.com/yadm-dev/yadm/2.5.0/yadm /usr/local/bin/yadm-2.5.0
RUN chmod +x /usr/local/bin/yadm-*
# Configure git to make it easier to test yadm manually

View file

@ -3,12 +3,12 @@
import collections
import contextlib
import copy
import distutils.dir_util # pylint: disable=no-name-in-module,import-error
import os
import platform
import pwd
from subprocess import Popen, PIPE
import py
import shutil
from subprocess import PIPE, Popen
import pytest
@ -22,157 +22,182 @@ def pytest_addoption(parser):
)
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def shellcheck_version():
"""Version of shellcheck supported"""
return '0.8.0'
return "0.10.0"
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def pylint_version():
"""Version of pylint supported"""
return '2.6.0'
return "3.3.1"
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def isort_version():
"""Version of isort supported"""
return "5.13.2"
@pytest.fixture(scope="session")
def flake8_version():
"""Version of flake8 supported"""
return '3.8.4'
return "7.1.1"
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def black_version():
"""Version of black supported"""
return "24.10.0"
@pytest.fixture(scope="session")
def yamllint_version():
"""Version of yamllint supported"""
return '1.25.0'
return "1.35.1"
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def tst_user():
"""Test session's user id"""
return pwd.getpwuid(os.getuid()).pw_name
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def tst_host():
"""Test session's short hostname value"""
return platform.node().split('.')[0]
return platform.node().split(".")[0]
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def tst_distro(runner):
"""Test session's distro"""
distro = ''
distro = ""
with contextlib.suppress(Exception):
run = runner(command=['lsb_release', '-si'], report=False)
run = runner(command=["lsb_release", "-si"], report=False)
distro = run.out.strip()
return distro
@pytest.fixture(scope='session')
def tst_distro_family(runner):
@pytest.fixture(scope="session")
def tst_distro_family():
"""Test session's distro_family"""
family = ''
family = ""
with contextlib.suppress(Exception):
run = runner(command=[
'grep', '-oP', r'ID_LIKE=\K.+', '/etc/os-release'], report=False)
family = run.out.strip()
return family
with open("/etc/os-release", encoding="utf-8") as f:
for line in f:
if line.startswith("ID_LIKE="):
family = line[8:]
break
if line.startswith("ID="):
family = line[3:]
# No break, only used as fallback in case ID_LIKE isn't found
return family.replace('"', "").rstrip()
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def tst_sys():
"""Test session's uname value"""
return platform.system()
system = platform.system()
if system == "Linux":
# Additional check for WSL
with open("/proc/version", encoding="utf-8") as f:
if "icrosoft" in f.read():
system = "WSL"
return system
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def tst_arch():
"""Test session's uname value"""
return platform.machine()
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def supported_commands():
"""List of supported commands
This list should be updated every time yadm learns a new command.
"""
return [
'alt',
'bootstrap',
'clean',
'clone',
'config',
'decrypt',
'encrypt',
'enter',
'git-crypt',
'gitconfig',
'help',
'init',
'introspect',
'list',
'perms',
'transcrypt',
'upgrade',
'version',
]
"alt",
"bootstrap",
"clean",
"clone",
"config",
"decrypt",
"encrypt",
"enter",
"git-crypt",
"gitconfig",
"help",
"init",
"introspect",
"list",
"perms",
"transcrypt",
"upgrade",
"version",
]
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def supported_configs():
"""List of supported config options
This list should be updated every time yadm learns a new config.
"""
return [
'local.arch',
'local.class',
'local.hostname',
'local.os',
'local.user',
'yadm.alt-copy',
'yadm.auto-alt',
'yadm.auto-exclude',
'yadm.auto-perms',
'yadm.auto-private-dirs',
'yadm.cipher',
'yadm.git-program',
'yadm.gpg-perms',
'yadm.gpg-program',
'yadm.gpg-recipient',
'yadm.openssl-ciphername',
'yadm.openssl-old',
'yadm.openssl-program',
'yadm.ssh-perms',
]
"local.arch",
"local.class",
"local.distro",
"local.distro-family",
"local.hostname",
"local.os",
"local.user",
"yadm.alt-copy",
"yadm.auto-alt",
"yadm.auto-exclude",
"yadm.auto-perms",
"yadm.auto-private-dirs",
"yadm.cipher",
"yadm.git-program",
"yadm.gpg-perms",
"yadm.gpg-program",
"yadm.gpg-recipient",
"yadm.openssl-ciphername",
"yadm.openssl-old",
"yadm.openssl-program",
"yadm.ssh-perms",
]
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def supported_switches():
"""List of supported switches
This list should be updated every time yadm learns a new switch.
"""
return [
'--yadm-archive',
'--yadm-bootstrap',
'--yadm-config',
'--yadm-data',
'--yadm-dir',
'--yadm-encrypt',
'--yadm-repo',
'-Y',
]
"--yadm-archive",
"--yadm-bootstrap",
"--yadm-config",
"--yadm-data",
"--yadm-dir",
"--yadm-encrypt",
"--yadm-repo",
"-Y",
]
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def supported_local_configs(supported_configs):
"""List of supported local config options"""
return [c for c in supported_configs if c.startswith('local.')]
return [c for c in supported_configs if c.startswith("local.")]
class Runner():
class Runner:
"""Class for running commands
Within yadm tests, this object should be used when running commands that
@ -185,17 +210,9 @@ class Runner():
Other instances of simply running commands should use os.system().
"""
def __init__(
self,
command,
inp=None,
shell=False,
cwd=None,
env=None,
expect=None,
report=True):
def __init__(self, command, inp=None, shell=False, cwd=None, env=None, expect=None, report=True):
if shell:
self.command = ' '.join([str(cmd) for cmd in command])
self.command = " ".join([str(cmd) for cmd in command])
else:
self.command = command
if env is None:
@ -204,7 +221,7 @@ class Runner():
merged_env.update(env)
self.inp = inp
self.wrap(expect)
process = Popen(
with Popen(
self.command,
stdin=PIPE,
stdout=PIPE,
@ -212,67 +229,57 @@ class Runner():
shell=shell,
cwd=cwd,
env=merged_env,
)
input_bytes = self.inp
if self.inp:
input_bytes = self.inp.encode()
(out_bstream, err_bstream) = process.communicate(input=input_bytes)
self.out = out_bstream.decode()
self.err = err_bstream.decode()
self.code = process.wait()
) as process:
input_bytes = self.inp
if self.inp:
input_bytes = self.inp.encode()
(out_bstream, err_bstream) = process.communicate(input=input_bytes)
self.out = out_bstream.decode()
self.err = err_bstream.decode()
self.code = process.wait()
self.success = self.code == 0
self.failure = self.code != 0
if report:
self.report()
def __repr__(self):
return f'Runner({self.command})'
return f"Runner({self.command})"
def report(self):
"""Print code/stdout/stderr"""
print(f'{self}')
print(f' RUN: code:{self.code}')
print(f"{self}")
print(f" RUN: code:{self.code}")
if self.inp:
print(f' RUN: input:\n{self.inp}')
print(f' RUN: stdout:\n{self.out}')
print(f' RUN: stderr:\n{self.err}')
print(f" RUN: input:\n{self.inp}")
print(f" RUN: stdout:\n{self.out}")
print(f" RUN: stderr:\n{self.err}")
def wrap(self, expect):
"""Wrap command with expect"""
if not expect:
return
cmdline = ' '.join([f'"{w}"' for w in self.command])
expect_script = f'set timeout 2\nspawn {cmdline}\n'
cmdline = " ".join([f'"{w}"' for w in self.command])
expect_script = f"set timeout 5\nspawn {cmdline}\n"
for question, answer in expect:
expect_script += (
'expect {\n'
f'"{question}" {{send "{answer}\\r"}}\n'
'timeout {close;exit 128}\n'
'}\n')
expect_script += (
'expect eof\n'
'foreach {pid spawnid os_error_flag value} [wait] break\n'
'exit $value')
expect_script += "expect {\n" f'"{question}" {{send "{answer}\\r"}}\n' "timeout {close;exit 128}\n" "}\n"
expect_script += "expect eof\n" "foreach {pid spawnid os_error_flag value} [wait] break\n" "exit $value"
self.inp = expect_script
print(f'EXPECT:{expect_script}')
self.command = ['expect']
print(f"EXPECT:{expect_script}")
self.command = ["expect"]
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def runner():
"""Class for running commands"""
return Runner
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def config_git():
"""Configure global git configuration, if missing"""
os.system(
'git config user.name || '
'git config --global user.name "test"')
os.system(
'git config user.email || '
'git config --global user.email "test@test.test"')
os.system("git config init.defaultBranch || git config --global init.defaultBranch master")
os.system('git config user.name || git config --global user.name "test"')
os.system('git config user.email || git config --global user.email "test@test.test"')
@pytest.fixture()
@ -282,19 +289,19 @@ def repo_config(runner, paths):
def query_func(key):
"""Query a yadm repo configuration value"""
run = runner(
command=('git', 'config', '--local', key),
env={'GIT_DIR': paths.repo},
command=("git", "config", "--local", key),
env={"GIT_DIR": paths.repo},
report=False,
)
)
return run.out.rstrip()
return query_func
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def yadm():
"""Path to yadm program to be tested"""
full_path = os.path.realpath('yadm')
full_path = os.path.realpath("yadm")
assert os.path.isfile(full_path), "yadm program file isn't present"
return full_path
@ -303,38 +310,40 @@ def yadm():
def paths(tmpdir, yadm):
"""Function scoped test paths"""
dir_root = tmpdir.mkdir('root')
dir_remote = dir_root.mkdir('remote')
dir_work = dir_root.mkdir('work')
dir_xdg_data = dir_root.mkdir('xdg_data')
dir_xdg_home = dir_root.mkdir('xdg_home')
dir_data = dir_xdg_data.mkdir('yadm')
dir_yadm = dir_xdg_home.mkdir('yadm')
dir_hooks = dir_yadm.mkdir('hooks')
dir_repo = dir_data.mkdir('repo.git')
file_archive = dir_data.join('archive')
file_bootstrap = dir_yadm.join('bootstrap')
file_config = dir_yadm.join('config')
file_encrypt = dir_yadm.join('encrypt')
dir_root = tmpdir.mkdir("root")
dir_remote = dir_root.mkdir("remote")
dir_work = dir_root.mkdir("work")
dir_xdg_data = dir_root.mkdir("xdg_data")
dir_xdg_home = dir_root.mkdir("xdg_home")
dir_data = dir_xdg_data.mkdir("yadm")
dir_yadm = dir_xdg_home.mkdir("yadm")
dir_hooks = dir_yadm.mkdir("hooks")
dir_repo = dir_data.mkdir("repo.git")
file_archive = dir_data.join("archive")
file_bootstrap = dir_yadm.join("bootstrap")
file_config = dir_yadm.join("config")
file_encrypt = dir_yadm.join("encrypt")
paths = collections.namedtuple(
'Paths', [
'pgm',
'root',
'remote',
'work',
'xdg_data',
'xdg_home',
'data',
'yadm',
'hooks',
'repo',
'archive',
'bootstrap',
'config',
'encrypt',
])
os.environ['XDG_CONFIG_HOME'] = str(dir_xdg_home)
os.environ['XDG_DATA_HOME'] = str(dir_xdg_data)
"Paths",
[
"pgm",
"root",
"remote",
"work",
"xdg_data",
"xdg_home",
"data",
"yadm",
"hooks",
"repo",
"archive",
"bootstrap",
"config",
"encrypt",
],
)
os.environ["XDG_CONFIG_HOME"] = str(dir_xdg_home)
os.environ["XDG_DATA_HOME"] = str(dir_xdg_data)
return paths(
yadm,
dir_root,
@ -350,19 +359,25 @@ def paths(tmpdir, yadm):
file_bootstrap,
file_config,
file_encrypt,
)
)
@pytest.fixture()
def yadm_cmd(paths):
"""Generate custom command_list function"""
def command_list(*args):
"""Produce params for running yadm with -Y"""
return [paths.pgm] + list(args)
return command_list
class DataFile():
class NoRelativePath(Exception):
"""Exception when finding relative paths"""
class DataFile:
"""Datafile object"""
def __init__(self, path, tracked=True, private=False):
@ -381,7 +396,7 @@ class DataFile():
"""Relative path property"""
if self.__parent:
return self.__parent.join(self.path)
raise BaseException('Unable to provide relative path, no parent')
raise NoRelativePath("Unable to provide relative path, no parent")
@property
def tracked(self):
@ -398,22 +413,18 @@ class DataFile():
self.__parent = parent
class DataSet():
class DataSet:
"""Dataset object"""
def __init__(self):
self.__files = list()
self.__dirs = list()
self.__tracked_dirs = list()
self.__private_dirs = list()
self.__files = []
self.__dirs = []
self.__tracked_dirs = []
self.__private_dirs = []
self.__relpath = None
def __repr__(self):
return (
f'[DS with {len(self)} files; '
f'{len(self.tracked)} tracked, '
f'{len(self.private)} private]'
)
return f"[DS with {len(self)} files; " f"{len(self.tracked)} tracked, " f"{len(self.private)} private]"
def __iter__(self):
return iter(self.__files)
@ -451,17 +462,17 @@ class DataSet():
@property
def plain_dirs(self):
"""List of directories in DataSet not starting with '.'"""
return [d for d in self.dirs if not d.startswith('.')]
return [d for d in self.dirs if not d.startswith(".")]
@property
def hidden_dirs(self):
"""List of directories in DataSet starting with '.'"""
return [d for d in self.dirs if d.startswith('.')]
return [d for d in self.dirs if d.startswith(".")]
@property
def tracked_dirs(self):
"""List of directories in DataSet not starting with '.'"""
return [d for d in self.__tracked_dirs if not d.startswith('.')]
return [d for d in self.__tracked_dirs if not d.startswith(".")]
@property
def private_dirs(self):
@ -491,23 +502,23 @@ class DataSet():
datafile.relative_to(self.__relpath)
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def ds1_dset(tst_sys):
"""Meta-data for dataset one files"""
dset = DataSet()
dset.add_file('t1')
dset.add_file('d1/t2')
dset.add_file(f'test_alt_copy##os.{tst_sys}')
dset.add_file('u1', tracked=False)
dset.add_file('d2/u2', tracked=False)
dset.add_file('.ssh/p1', tracked=False, private=True)
dset.add_file('.ssh/.p2', tracked=False, private=True)
dset.add_file('.gnupg/p3', tracked=False, private=True)
dset.add_file('.gnupg/.p4', tracked=False, private=True)
dset.add_file("t1")
dset.add_file("d1/t2")
dset.add_file(f"test_alt_copy##os.{tst_sys}")
dset.add_file("u1", tracked=False)
dset.add_file("d2/u2", tracked=False)
dset.add_file(".ssh/p1", tracked=False, private=True)
dset.add_file(".ssh/.p2", tracked=False, private=True)
dset.add_file(".gnupg/p3", tracked=False, private=True)
dset.add_file(".gnupg/.p4", tracked=False, private=True)
return dset
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def ds1_data(tmpdir_factory, config_git, ds1_dset, runner):
"""A set of test data, worktree & repo"""
# pylint: disable=unused-argument
@ -515,65 +526,40 @@ def ds1_data(tmpdir_factory, config_git, ds1_dset, runner):
# @pytest.mark.usefixtures('config_git')
# cannot be applied to another fixture.
data = tmpdir_factory.mktemp('ds1')
data = tmpdir_factory.mktemp("ds1")
work = data.mkdir('work')
work = data.mkdir("work")
for datafile in ds1_dset:
work.join(datafile.path).write(datafile.path, ensure=True)
repo = data.mkdir('repo.git')
repo = data.mkdir("repo.git")
env = os.environ.copy()
env['GIT_DIR'] = str(repo)
runner(
command=['git', 'init', '--shared=0600', '--bare', str(repo)],
report=False)
runner(
command=['git', 'config', 'core.bare', 'false'],
env=env,
report=False)
runner(
command=['git', 'config', 'status.showUntrackedFiles', 'no'],
env=env,
report=False)
runner(
command=['git', 'config', 'yadm.managed', 'true'],
env=env,
report=False)
runner(
command=['git', 'config', 'core.worktree', str(work)],
env=env,
report=False)
runner(
command=['git', 'add'] +
[str(work.join(f.path)) for f in ds1_dset if f.tracked],
env=env)
runner(
command=['git', 'commit', '--allow-empty', '-m', 'Initial commit'],
env=env,
report=False)
env["GIT_DIR"] = str(repo)
runner(command=["git", "init", "--shared=0600", "--bare", str(repo)], report=False)
runner(command=["git", "config", "core.bare", "false"], env=env, report=False)
runner(command=["git", "config", "status.showUntrackedFiles", "no"], env=env, report=False)
runner(command=["git", "config", "yadm.managed", "true"], env=env, report=False)
runner(command=["git", "config", "core.worktree", str(work)], env=env, report=False)
runner(command=["git", "add"] + [str(work.join(f.path)) for f in ds1_dset if f.tracked], env=env)
runner(command=["git", "commit", "--allow-empty", "-m", "Initial commit"], env=env, report=False)
data = collections.namedtuple('Data', ['work', 'repo'])
data = collections.namedtuple("Data", ["work", "repo"])
return data(work, repo)
@pytest.fixture()
def ds1_work_copy(ds1_data, paths):
"""Function scoped copy of ds1_data.work"""
distutils.dir_util.copy_tree( # pylint: disable=no-member
str(ds1_data.work), str(paths.work))
shutil.copytree(str(ds1_data.work), str(paths.work), dirs_exist_ok=True)
@pytest.fixture()
def ds1_repo_copy(runner, ds1_data, paths):
"""Function scoped copy of ds1_data.repo"""
distutils.dir_util.copy_tree( # pylint: disable=no-member
str(ds1_data.repo), str(paths.repo))
shutil.copytree(str(ds1_data.repo), str(paths.repo), dirs_exist_ok=True)
env = os.environ.copy()
env['GIT_DIR'] = str(paths.repo)
runner(
command=['git', 'config', 'core.worktree', str(paths.work)],
env=env,
report=False)
env["GIT_DIR"] = str(paths.repo)
runner(command=["git", "config", "core.worktree", str(paths.work)], env=env, report=False)
@pytest.fixture()
@ -598,30 +584,39 @@ def ds1(ds1_work_copy, paths, ds1_dset):
return dscopy
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def gnupg(tmpdir_factory, runner):
"""Location of GNUPGHOME"""
def register_gpg_password(password):
"""Publish a new GPG mock password"""
py.path.local('/tmp/mock-password').write(password)
home = tmpdir_factory.mktemp('gnupghome')
home = tmpdir_factory.mktemp("gnupghome")
home.chmod(0o700)
conf = home.join('gpg.conf')
conf.write('no-secmem-warning\n')
conf = home.join("gpg.conf")
conf.write("no-secmem-warning\n")
conf.chmod(0o600)
agentconf = home.join('gpg-agent.conf')
agentconf = home.join("gpg-agent.conf")
agentconf.write(
f'pinentry-program {os.path.abspath("test/pinentry-mock")}\n'
'max-cache-ttl 0\n'
f"""\
pinentry-program {os.path.abspath("test/pinentry-mock")}
max-cache-ttl 0
browser-socket none
extra-socket none
disable-scdaemon
"""
)
agentconf.chmod(0o600)
data = collections.namedtuple('GNUPG', ['home', 'pw'])
data = collections.namedtuple("GNUPG", ["home", "pw"])
env = os.environ.copy()
env['GNUPGHOME'] = home
env["GNUPGHOME"] = home
# this pre-populates std files in the GNUPGHOME
runner(['gpg', '-k'], env=env)
runner(["gpg", "-k"], env=env, report=False)
return data(home, register_gpg_password)
def register_gpg_password(password):
"""Publish a new GPG mock password and flush cached passwords"""
home.join("mock-password").write(password)
runner(["gpgconf", "--reload", "gpg-agent"], env=env, report=False)
yield data(home, register_gpg_password)
runner(["gpgconf", "--kill", "gpg-agent"], env=env, report=False)
runner(["gpgconf", "--remove-socketdir", "gpg-agent"], env=env, report=False)

View file

@ -1,12 +1,19 @@
#!/bin/bash
# This program is a custom mock pinentry program
# It always uses whatever password is found in the /tmp directory
password="$(cat /tmp/mock-password 2>/dev/null)"
# It uses whatever password is found in the /tmp directory
# If the password is empty, replies CANCEL causing an error to similate invalid
# credentials
echo "OK Pleased to meet you"
while read -r line; do
if [[ $line =~ GETPIN ]]; then
echo -n "D "
echo "$password"
password="$(cat "$GNUPGHOME/mock-password" 2>/dev/null)"
if [ -n "$password" ]; then
echo "D $password"
echo "OK";
else
echo "CANCEL";
fi
else
echo "OK";
fi
echo "OK";
done

View file

@ -1 +0,0 @@
../pylintrc

View file

@ -1,6 +1,8 @@
black==24.10.0
envtpl
flake8==3.8.4
flake8==7.1.1
isort==5.13.2
j2cli
pylint==2.6.0
pytest==6.2.1
yamllint==1.25.0
pylint==3.3.1
pytest==8.3.3
yamllint==1.35.1

View file

@ -1,6 +1,8 @@
"""Test alt"""
import os
import string
import py
import pytest
import utils
@ -8,34 +10,34 @@ import utils
TEST_PATHS = [utils.ALT_FILE1, utils.ALT_FILE2, utils.ALT_DIR]
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('yadm_alt', [True, False], ids=['alt', 'worktree'])
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
@pytest.mark.parametrize(
'tracked,encrypt,exclude', [
"tracked,encrypt,exclude",
[
(False, False, False),
(True, False, False),
(False, True, False),
(False, True, True),
], ids=['untracked', 'tracked', 'encrypted', 'excluded'])
def test_alt_source(
runner, paths,
tracked, encrypt, exclude,
yadm_alt):
],
ids=["untracked", "tracked", "encrypted", "excluded"],
)
def test_alt_source(runner, paths, tracked, encrypt, exclude, yadm_alt):
"""Test yadm alt operates on all expected sources of alternates"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
utils.create_alt_files(
paths, '##default', tracked=tracked, encrypt=encrypt, exclude=exclude,
yadm_alt=yadm_alt, yadm_dir=yadm_dir)
run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt'])
paths, "##default", tracked=tracked, encrypt=encrypt, exclude=exclude, yadm_alt=yadm_alt, yadm_dir=yadm_dir
)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ''
assert run.err == ""
linked = utils.parse_alt_output(run.out)
basepath = yadm_dir.join('alt') if yadm_alt else paths.work
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
for link_path in TEST_PATHS:
source_file_content = link_path + '##default'
source_file_content = link_path + "##default"
source_file = basepath.join(source_file_content)
link_file = paths.work.join(link_path)
if tracked or (encrypt and not exclude):
@ -45,63 +47,69 @@ def test_alt_source(
assert link_file.read() == source_file_content
assert str(source_file) in linked
else:
assert link_file.join(
utils.CONTAINED).read() == source_file_content
assert link_file.join(utils.CONTAINED).read() == source_file_content
assert str(source_file) in linked
else:
assert not link_file.exists()
assert str(source_file) not in linked
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('yadm_alt', [True, False], ids=['alt', 'worktree'])
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
def test_relative_link(runner, paths, yadm_alt):
"""Confirm links created are relative"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
utils.create_alt_files(
paths, '##default', tracked=True, encrypt=False, exclude=False,
yadm_alt=yadm_alt, yadm_dir=yadm_dir)
run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt'])
paths, "##default", tracked=True, encrypt=False, exclude=False, yadm_alt=yadm_alt, yadm_dir=yadm_dir
)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ''
assert run.err == ""
basepath = yadm_dir.join('alt') if yadm_alt else paths.work
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
for link_path in TEST_PATHS:
source_file_content = link_path + '##default'
source_file_content = link_path + "##default"
source_file = basepath.join(source_file_content)
link_file = paths.work.join(link_path)
link = link_file.readlink()
relpath = os.path.relpath(
source_file, start=os.path.dirname(link_file))
relpath = os.path.relpath(source_file, start=os.path.dirname(link_file))
assert link == relpath
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('suffix', [
'##default',
'##default,e.txt', '##default,extension.txt',
'##a.$tst_arch', '##arch.$tst_arch',
'##o.$tst_sys', '##os.$tst_sys',
'##d.$tst_distro', '##distro.$tst_distro',
'##f.$tst_distro_family', '##distro_family.$tst_distro_family',
'##c.$tst_class', '##class.$tst_class',
'##h.$tst_host', '##hostname.$tst_host',
'##u.$tst_user', '##user.$tst_user',
])
def test_alt_conditions(
runner, paths,
tst_arch, tst_sys, tst_distro, tst_distro_family, tst_host, tst_user,
suffix):
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize(
"suffix",
[
"##default",
"##default,e.txt",
"##default,extension.txt",
"##a.$tst_arch",
"##arch.$tst_arch",
"##o.$tst_sys",
"##os.$tst_sys",
"##d.$tst_distro",
"##distro.$tst_distro",
"##f.$tst_distro_family",
"##distro_family.$tst_distro_family",
"##c.$tst_class",
"##class.$tst_class",
"##h.$tst_host",
"##hostname.$tst_host",
"##u.$tst_user",
"##user.$tst_user",
],
)
def test_alt_conditions(runner, paths, tst_arch, tst_sys, tst_distro, tst_distro_family, tst_host, tst_user, suffix):
"""Test conditions supported by yadm alt"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
# set the class
tst_class = 'testclass'
utils.set_local(paths, 'class', tst_class + ".before")
utils.set_local(paths, 'class', tst_class, add=True)
utils.set_local(paths, 'class', tst_class + ".after", add=True)
tst_class = "testclass"
utils.set_local(paths, "class", tst_class + ".before")
utils.set_local(paths, "class", tst_class, add=True)
utils.set_local(paths, "class", tst_class + ".after", add=True)
suffix = string.Template(suffix).substitute(
tst_arch=tst_arch,
@ -114,9 +122,9 @@ def test_alt_conditions(
)
utils.create_alt_files(paths, suffix)
run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt'])
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ''
assert run.err == ""
linked = utils.parse_alt_output(run.out)
for link_path in TEST_PATHS:
@ -127,27 +135,31 @@ def test_alt_conditions(
assert paths.work.join(link_path).read() == source_file
assert str(paths.work.join(source_file)) in linked
else:
assert paths.work.join(link_path).join(
utils.CONTAINED).read() == source_file
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
assert str(paths.work.join(source_file)) in linked
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("kind", ["default", "", None, "envtpl", "j2cli", "j2", "esh"])
@pytest.mark.parametrize(
'kind', ['default', '', None, 'envtpl', 'j2cli', 'j2', 'esh'])
@pytest.mark.parametrize('label', ['t', 'template', 'yadm', ])
def test_alt_templates(
runner, paths, kind, label):
"label",
[
"t",
"template",
"yadm",
],
)
def test_alt_templates(runner, paths, kind, label):
"""Test templates supported by yadm alt"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
suffix = f'##{label}.{kind}'
suffix = f"##{label}.{kind}"
if kind is None:
suffix = f'##{label}'
suffix = f"##{label}"
utils.create_alt_files(paths, suffix)
run = runner([paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'alt'])
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ''
assert run.err == ""
created = utils.parse_alt_output(run.out, linked=False)
for created_path in TEST_PATHS:
@ -158,41 +170,77 @@ def test_alt_templates(
assert str(paths.work.join(source_file)) in created
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('autoalt', [None, 'true', 'false'])
@pytest.mark.usefixtures("ds1_copy")
def test_alt_template_with_condition(runner, paths, tst_arch):
"""Test template with extra condition"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
suffix = f"##template,arch.not{tst_arch}"
utils.create_alt_files(paths, suffix)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ""
created = utils.parse_alt_output(run.out, linked=False)
assert len(created) == 0
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("autoalt", [None, "true", "false"])
def test_auto_alt(runner, yadm_cmd, paths, autoalt):
"""Test auto alt"""
# set the value of auto-alt
if autoalt:
os.system(' '.join(yadm_cmd('config', 'yadm.auto-alt', autoalt)))
os.system(" ".join(yadm_cmd("config", "yadm.auto-alt", autoalt)))
utils.create_alt_files(paths, '##default')
run = runner(yadm_cmd('status'))
utils.create_alt_files(paths, "##default")
run = runner(yadm_cmd("status"))
assert run.success
assert run.err == ''
assert run.err == ""
linked = utils.parse_alt_output(run.out)
for link_path in TEST_PATHS:
source_file = link_path + '##default'
if autoalt == 'false':
source_file = link_path + "##default"
if autoalt == "false":
assert not paths.work.join(link_path).exists()
else:
assert paths.work.join(link_path).islink()
target = py.path.local(
os.path.realpath(paths.work.join(link_path)))
target = py.path.local(os.path.realpath(paths.work.join(link_path)))
if target.isfile():
assert paths.work.join(link_path).read() == source_file
# no linking output when run via auto-alt
assert str(paths.work.join(source_file)) not in linked
else:
assert paths.work.join(link_path).join(
utils.CONTAINED).read() == source_file
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
# no linking output when run via auto-alt
assert str(paths.work.join(source_file)) not in linked
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("autoexclude", [None, "true", "false"])
def test_alt_exclude(runner, yadm_cmd, paths, autoexclude):
"""Test alt exclude"""
# set the value of auto-exclude
if autoexclude:
os.system(" ".join(yadm_cmd("config", "yadm.auto-exclude", autoexclude)))
utils.create_alt_files(paths, "##default")
run = runner(yadm_cmd("alt", "-d"))
assert run.success
run = runner(yadm_cmd("status", "-z", "-uall", "--ignored"))
assert run.success
assert run.err == ""
status = run.out.split("\0")
for link_path in TEST_PATHS:
flags = "??" if autoexclude == "false" else "!!"
assert f"{flags} {link_path}" in status
@pytest.mark.usefixtures("ds1_copy")
def test_stale_link_removal(runner, yadm_cmd, paths):
"""Stale links to alternative files are removed
@ -201,48 +249,47 @@ def test_stale_link_removal(runner, yadm_cmd, paths):
"""
# set the class
tst_class = 'testclass'
utils.set_local(paths, 'class', tst_class)
tst_class = "testclass"
utils.set_local(paths, "class", tst_class)
# create files which match the test class
utils.create_alt_files(paths, f'##class.{tst_class}')
utils.create_alt_files(paths, f"##class.{tst_class}")
# run alt to trigger linking
run = runner(yadm_cmd('alt'))
run = runner(yadm_cmd("alt"))
assert run.success
assert run.err == ''
assert run.err == ""
linked = utils.parse_alt_output(run.out)
# assert the proper linking has occurred
for stale_path in TEST_PATHS:
source_file = stale_path + '##class.' + tst_class
source_file = stale_path + "##class." + tst_class
assert paths.work.join(stale_path).islink()
target = py.path.local(os.path.realpath(paths.work.join(stale_path)))
if target.isfile():
assert paths.work.join(stale_path).read() == source_file
assert str(paths.work.join(source_file)) in linked
else:
assert paths.work.join(stale_path).join(
utils.CONTAINED).read() == source_file
assert paths.work.join(stale_path).join(utils.CONTAINED).read() == source_file
assert str(paths.work.join(source_file)) in linked
# change the class so there are no valid alternates
utils.set_local(paths, 'class', 'changedclass')
utils.set_local(paths, "class", "changedclass")
# run alt to trigger linking
run = runner(yadm_cmd('alt'))
run = runner(yadm_cmd("alt"))
assert run.success
assert run.err == ''
assert run.err == ""
linked = utils.parse_alt_output(run.out)
# assert the linking is removed
for stale_path in TEST_PATHS:
source_file = stale_path + '##class.' + tst_class
source_file = stale_path + "##class." + tst_class
assert not paths.work.join(stale_path).exists()
assert str(paths.work.join(source_file)) not in linked
@pytest.mark.usefixtures('ds1_repo_copy')
@pytest.mark.usefixtures("ds1_repo_copy")
def test_template_overwrite_symlink(runner, yadm_cmd, paths, tst_sys):
"""Remove symlinks before processing a template
@ -251,45 +298,44 @@ def test_template_overwrite_symlink(runner, yadm_cmd, paths, tst_sys):
be removed just before processing a template.
"""
target = paths.work.join(f'test_link##os.{tst_sys}')
target.write('target')
target = paths.work.join(f"test_link##os.{tst_sys}")
target.write("target")
link = paths.work.join('test_link')
link = paths.work.join("test_link")
link.mksymlinkto(target, absolute=1)
template = paths.work.join('test_link##template.default')
template.write('test-data')
template = paths.work.join("test_link##template.default")
template.write("test-data")
run = runner(yadm_cmd('add', target, template))
run = runner(yadm_cmd("add", target, template))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
assert not link.islink()
assert target.read().strip() == 'target'
assert link.read().strip() == 'test-data'
assert target.read().strip() == "target"
assert link.read().strip() == "test-data"
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize('style', ['symlink', 'template'])
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("style", ["symlink", "template"])
def test_ensure_alt_path(runner, paths, style):
"""Test that directories are created before making alternates"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
suffix = 'default' if style == 'symlink' else 'template'
filename = 'a/b/c/file'
source = yadm_dir.join(f'alt/{filename}##{suffix}')
source.write('test-data', ensure=True)
run = runner([
paths.pgm, '-Y', yadm_dir, '--yadm-data', yadm_data, 'add', source])
suffix = "default" if style == "symlink" else "template"
filename = "a/b/c/file"
source = yadm_dir.join(f"alt/{filename}##{suffix}")
source.write("test-data", ensure=True)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "add", source])
assert run.success
assert run.err == ''
assert run.out == ''
assert paths.work.join(filename).read().strip() == 'test-data'
assert run.err == ""
assert run.out == ""
assert paths.work.join(filename).read().strip() == "test-data"
def setup_standard_yadm_dir(paths):
"""Configure a yadm home within the work tree"""
std_yadm_dir = paths.work.mkdir('.config').mkdir('yadm')
std_yadm_data = paths.work.mkdir('.local').mkdir('share').mkdir('yadm')
std_yadm_data.join('repo.git').mksymlinkto(paths.repo, absolute=1)
std_yadm_dir.join('encrypt').mksymlinkto(paths.encrypt, absolute=1)
std_yadm_dir = paths.work.mkdir(".config").mkdir("yadm")
std_yadm_data = paths.work.mkdir(".local").mkdir("share").mkdir("yadm")
std_yadm_data.join("repo.git").mksymlinkto(paths.repo, absolute=1)
std_yadm_dir.join("encrypt").mksymlinkto(paths.encrypt, absolute=1)
return std_yadm_dir, std_yadm_data

View file

@ -1,45 +1,47 @@
"""Test yadm.alt-copy"""
import os
import pytest
@pytest.mark.parametrize(
'setting, expect_link, pre_existing', [
"setting, expect_link, pre_existing",
[
(None, True, None),
(True, False, None),
(False, True, None),
(True, False, 'link'),
(True, False, 'file'),
(True, False, "link"),
(True, False, "file"),
],
ids=[
'unset',
'true',
'false',
'pre-existing symlink',
'pre-existing file',
])
@pytest.mark.usefixtures('ds1_copy')
def test_alt_copy(
runner, yadm_cmd, paths, tst_sys,
setting, expect_link, pre_existing):
"unset",
"true",
"false",
"pre-existing symlink",
"pre-existing file",
],
)
@pytest.mark.usefixtures("ds1_copy")
def test_alt_copy(runner, yadm_cmd, paths, tst_sys, setting, expect_link, pre_existing):
"""Test yadm.alt-copy"""
if setting is not None:
os.system(' '.join(yadm_cmd('config', 'yadm.alt-copy', str(setting))))
os.system(" ".join(yadm_cmd("config", "yadm.alt-copy", str(setting))))
expected_content = f'test_alt_copy##os.{tst_sys}'
expected_content = f"test_alt_copy##os.{tst_sys}"
alt_path = paths.work.join('test_alt_copy')
if pre_existing == 'symlink':
alt_path = paths.work.join("test_alt_copy")
if pre_existing == "symlink":
alt_path.mklinkto(expected_content)
elif pre_existing == 'file':
alt_path.write('wrong content')
elif pre_existing == "file":
alt_path.write("wrong content")
run = runner(yadm_cmd('alt'))
run = runner(yadm_cmd("alt"))
assert run.success
assert run.err == ''
assert 'Linking' in run.out
assert run.err == ""
action = "Copying" if setting is True else "Linking"
assert action in run.out
assert alt_path.read() == expected_content
assert alt_path.islink() == expect_link

View file

@ -2,13 +2,14 @@
import os
import re
import pytest
pytestmark = pytest.mark.usefixtures('ds1_copy')
PRIVATE_DIRS = ['.gnupg', '.ssh']
pytestmark = pytest.mark.usefixtures("ds1_copy")
PRIVATE_DIRS = [".gnupg", ".ssh"]
@pytest.mark.parametrize('home', [True, False], ids=['home', 'not-home'])
@pytest.mark.parametrize("home", [True, False], ids=["home", "not-home"])
def test_pdirs_missing(runner, yadm_cmd, paths, home):
"""Private dirs (private dirs missing)
@ -24,15 +25,15 @@ def test_pdirs_missing(runner, yadm_cmd, paths, home):
path.remove()
assert not path.exists()
env = {'DEBUG': 'yes'}
env = {"DEBUG": "yes"}
if home:
env['HOME'] = paths.work
env["HOME"] = paths.work
# run status
run = runner(command=yadm_cmd('status'), env=env)
run = runner(command=yadm_cmd("status"), env=env)
assert run.success
assert run.err == ''
assert 'On branch master' in run.out
assert run.err == ""
assert "On branch master" in run.out
# confirm directories are created
# and are protected
@ -40,17 +41,15 @@ def test_pdirs_missing(runner, yadm_cmd, paths, home):
path = paths.work.join(pdir)
if home:
assert path.exists()
assert oct(path.stat().mode).endswith('00'), ('Directory is '
'not secured')
assert oct(path.stat().mode).endswith("00"), "Directory is " "not secured"
else:
assert not path.exists()
# confirm directories are created before command is run:
if home:
assert re.search(
(r'Creating.+\.(gnupg|ssh).+Creating.+\.(gnupg|ssh).+'
r'Running git command git status'),
run.out, re.DOTALL), 'directories created before command is run'
r"Creating.+\.(gnupg|ssh).+Creating.+\.(gnupg|ssh).+Running git command git status", run.out, re.DOTALL
), "directories created before command is run"
def test_pdirs_missing_apd_false(runner, yadm_cmd, paths):
@ -70,14 +69,13 @@ def test_pdirs_missing_apd_false(runner, yadm_cmd, paths):
assert not path.exists()
# set configuration
os.system(' '.join(yadm_cmd(
'config', '--bool', 'yadm.auto-private-dirs', 'false')))
os.system(" ".join(yadm_cmd("config", "--bool", "yadm.auto-private-dirs", "false")))
# run status
run = runner(command=yadm_cmd('status'))
run = runner(command=yadm_cmd("status"))
assert run.success
assert run.err == ''
assert 'On branch master' in run.out
assert run.err == ""
assert "On branch master" in run.out
# confirm directories are STILL missing
for pdir in PRIVATE_DIRS:
@ -99,19 +97,18 @@ def test_pdirs_exist_apd_false(runner, yadm_cmd, paths):
if not path.isdir():
path.mkdir()
path.chmod(0o777)
assert oct(path.stat().mode).endswith('77'), 'Directory is secure.'
assert oct(path.stat().mode).endswith("77"), "Directory is secure."
# set configuration
os.system(' '.join(yadm_cmd(
'config', '--bool', 'yadm.auto-perms', 'false')))
os.system(" ".join(yadm_cmd("config", "--bool", "yadm.auto-perms", "false")))
# run status
run = runner(command=yadm_cmd('status'))
run = runner(command=yadm_cmd("status"))
assert run.success
assert run.err == ''
assert 'On branch master' in run.out
assert run.err == ""
assert "On branch master" in run.out
# created directories are STILL permissive
for pdir in PRIVATE_DIRS:
path = paths.work.join(pdir)
assert oct(path.stat().mode).endswith('77'), 'Directory is secure'
assert oct(path.stat().mode).endswith("77"), "Directory is secure"

View file

@ -4,32 +4,30 @@ import pytest
@pytest.mark.parametrize(
'exists, executable, code, expect', [
(False, False, 1, 'Cannot execute bootstrap'),
(True, False, 1, 'is not an executable program'),
(True, True, 123, 'Bootstrap successful'),
], ids=[
'missing',
'not executable',
'executable',
])
def test_bootstrap(
runner, yadm_cmd, paths, exists, executable, code, expect):
"exists, executable, code, expect",
[
(False, False, 1, "Cannot execute bootstrap"),
(True, False, 1, "is not an executable program"),
(True, True, 123, "Bootstrap successful"),
],
ids=[
"missing",
"not executable",
"executable",
],
)
def test_bootstrap(runner, yadm_cmd, paths, exists, executable, code, expect):
"""Test bootstrap command"""
if exists:
paths.bootstrap.write('')
paths.bootstrap.write("")
if executable:
paths.bootstrap.write(
'#!/bin/bash\n'
f'echo {expect}\n'
f'exit {code}\n'
)
paths.bootstrap.write("#!/bin/bash\n" f"echo {expect}\n" f"exit {code}\n")
paths.bootstrap.chmod(0o775)
run = runner(command=yadm_cmd('bootstrap'))
run = runner(command=yadm_cmd("bootstrap"))
assert run.code == code
if exists and executable:
assert run.err == ''
assert run.err == ""
assert expect in run.out
else:
assert expect in run.err
assert run.out == ''
assert run.out == ""

View file

@ -3,9 +3,9 @@
def test_clean_command(runner, yadm_cmd):
"""Run with clean command"""
run = runner(command=yadm_cmd('clean'))
run = runner(command=yadm_cmd("clean"))
# do nothing, this is a dangerous Git command when managing dot files
# report the command as disabled and exit as a failure
assert run.failure
assert run.out == ''
assert 'disabled' in run.err
assert run.out == ""
assert "disabled" in run.err

View file

@ -2,30 +2,32 @@
import os
import re
import pytest
BOOTSTRAP_CODE = 123
BOOTSTRAP_MSG = 'Bootstrap successful'
BOOTSTRAP_MSG = "Bootstrap successful"
@pytest.mark.usefixtures('remote')
@pytest.mark.usefixtures("remote")
@pytest.mark.parametrize(
'good_remote, repo_exists, force, conflicts', [
"good_remote, repo_exists, force, conflicts",
[
(False, False, False, False),
(True, False, False, False),
(True, True, False, False),
(True, True, True, False),
(True, False, False, True),
], ids=[
'bad remote',
'simple',
'existing repo',
'-f',
'conflicts',
])
def test_clone(
runner, paths, yadm_cmd, repo_config, ds1,
good_remote, repo_exists, force, conflicts):
],
ids=[
"bad remote",
"simple",
"existing repo",
"-f",
"conflicts",
],
)
def test_clone(runner, paths, yadm_cmd, repo_config, ds1, good_remote, repo_exists, force, conflicts):
"""Test basic clone operation"""
# clear out the work path
@ -33,74 +35,70 @@ def test_clone(
paths.work.mkdir()
# determine remote url
remote_url = f'file://{paths.remote}'
remote_url = f"file://{paths.remote}"
if not good_remote:
remote_url = 'file://bad_remote'
remote_url = "file://bad_remote"
old_repo = None
if repo_exists:
# put a repo in the way
paths.repo.mkdir()
old_repo = paths.repo.join('old_repo')
old_repo.write('old_repo')
old_repo = paths.repo.join("old_repo")
old_repo.write("old_repo")
if conflicts:
ds1.tracked[0].relative.write('conflict')
ds1.tracked[0].relative.write("conflict")
assert ds1.tracked[0].relative.exists()
# run the clone command
args = ['clone', '-w', paths.work]
args = ["clone", "-w", paths.work]
if force:
args += ['-f']
args += ["-f"]
args += [remote_url]
run = runner(command=yadm_cmd(*args))
if not good_remote:
# clone should fail
assert run.failure
assert run.out == ''
assert 'Unable to clone the repository' in run.err
assert run.out == ""
assert "Unable to clone the repository" in run.err
assert not paths.repo.exists()
elif repo_exists and not force:
# can't overwrite data
assert run.failure
assert run.out == ''
assert 'Git repo already exists' in run.err
assert run.out == ""
assert "Git repo already exists" in run.err
else:
# clone should succeed, and repo should be configured properly
assert successful_clone(run, paths, repo_config)
# these clones should have master as HEAD
verify_head(paths, 'master')
verify_head(paths, "master")
# ensure conflicts are handled properly
if conflicts:
assert 'NOTE' in run.out
assert 'Local files with content that differs' in run.out
assert "NOTE" in run.out
assert "Local files with content that differs" in run.out
# confirm correct Git origin
run = runner(
command=('git', 'remote', '-v', 'show'),
env={'GIT_DIR': paths.repo})
run = runner(command=("git", "remote", "-v", "show"), env={"GIT_DIR": paths.repo})
assert run.success
assert run.err == ''
assert f'origin\t{remote_url}' in run.out
assert run.err == ""
assert f"origin\t{remote_url}" in run.out
# ensure conflicts are really preserved
if conflicts:
# test that the conflicts are preserved in the work tree
run = runner(
command=yadm_cmd('status', '-uno', '--porcelain'),
cwd=paths.work)
run = runner(command=yadm_cmd("status", "-uno", "--porcelain"), cwd=paths.work)
assert run.success
assert run.err == ''
assert run.err == ""
assert str(ds1.tracked[0].path) in run.out
# verify content of the conflicts
run = runner(command=yadm_cmd('diff'), cwd=paths.work)
run = runner(command=yadm_cmd("diff"), cwd=paths.work)
assert run.success
assert run.err == ''
assert '\n+conflict' in run.out, 'conflict overwritten'
assert run.err == ""
assert "\n+conflict" in run.out, "conflict overwritten"
# another force-related assertion
if old_repo:
@ -110,54 +108,89 @@ def test_clone(
assert old_repo.exists()
@pytest.mark.usefixtures('remote')
@pytest.mark.usefixtures("remote_with_submodules")
@pytest.mark.parametrize("action", ["recursive", "recurse", "specific"])
def test_clone_submodules(runner, paths, yadm_cmd, repo_config, action):
"""Test clone operation with submodules"""
# clear out the work path
paths.work.remove()
paths.work.mkdir()
env = {
"GIT_CONFIG_COUNT": "1",
"GIT_CONFIG_KEY_0": "protocol.file.allow",
"GIT_CONFIG_VALUE_0": "always",
}
args = ["clone", "-w", paths.work]
if action == "recursive":
args += ["--recursive"]
elif action == "recurse":
args += ["--recurse-submodules"]
elif action == "specific":
args += ["--recurse-submodules=a", "--recurse-submodules=d1/c"]
args += [f"file://{paths.remote}"]
run = runner(command=yadm_cmd(*args), env=env)
assert successful_clone(run, paths, repo_config)
for path in ("a", "b", "d1/c"):
if action != "specific" or path != "b":
assert paths.work.join(path).join(".git").exists()
else:
assert not paths.work.join(path).join(".git").exists()
@pytest.mark.usefixtures("remote")
@pytest.mark.parametrize(
'bs_exists, bs_param, answer', [
(False, '--bootstrap', None),
(True, '--bootstrap', None),
(True, '--no-bootstrap', None),
(True, None, 'n'),
(True, None, 'y'),
], ids=[
'force, missing',
'force, existing',
'prevent',
'existing, answer n',
'existing, answer y',
])
def test_clone_bootstrap(
runner, paths, yadm_cmd, repo_config, bs_exists, bs_param, answer):
"bs_exists, bs_param, answer",
[
(False, "--bootstrap", None),
(True, "--bootstrap", None),
(True, "--no-bootstrap", None),
(True, None, "n"),
(True, None, "y"),
],
ids=[
"force, missing",
"force, existing",
"prevent",
"existing, answer n",
"existing, answer y",
],
)
def test_clone_bootstrap(runner, paths, yadm_cmd, repo_config, bs_exists, bs_param, answer):
"""Test bootstrap clone features"""
# establish a bootstrap
create_bootstrap(paths, bs_exists)
# run the clone command
args = ['clone', '-w', paths.work]
args = ["clone", "-w", paths.work]
if bs_param:
args += [bs_param]
args += [f'file://{paths.remote}']
args += [f"file://{paths.remote}"]
expect = []
if answer:
expect.append(('Would you like to execute it now', answer))
expect.append(("Would you like to execute it now", answer))
run = runner(command=yadm_cmd(*args), expect=expect)
if answer:
assert 'Would you like to execute it now' in run.out
assert "Would you like to execute it now" in run.out
expected_code = 0
if bs_exists and bs_param != '--no-bootstrap':
if bs_exists and bs_param != "--no-bootstrap":
expected_code = BOOTSTRAP_CODE
if answer == 'y':
if answer == "y":
expected_code = BOOTSTRAP_CODE
assert BOOTSTRAP_MSG in run.out
elif answer == 'n':
elif answer == "n":
expected_code = 0
assert BOOTSTRAP_MSG not in run.out
assert successful_clone(run, paths, repo_config, expected_code)
verify_head(paths, 'master')
verify_head(paths, "master")
if not bs_exists:
assert BOOTSTRAP_MSG not in run.out
@ -166,108 +199,90 @@ def test_clone_bootstrap(
def create_bootstrap(paths, exists):
"""Create bootstrap file for test"""
if exists:
paths.bootstrap.write(
'#!/bin/sh\n'
f'echo {BOOTSTRAP_MSG}\n'
f'exit {BOOTSTRAP_CODE}\n')
paths.bootstrap.write("#!/bin/sh\n" f"echo {BOOTSTRAP_MSG}\n" f"exit {BOOTSTRAP_CODE}\n")
paths.bootstrap.chmod(0o775)
assert paths.bootstrap.exists()
else:
assert not paths.bootstrap.exists()
@pytest.mark.usefixtures('remote')
@pytest.mark.usefixtures("remote")
@pytest.mark.parametrize(
'private_type, in_repo, in_work', [
('ssh', False, True),
('gnupg', False, True),
('ssh', True, True),
('gnupg', True, True),
('ssh', True, False),
('gnupg', True, False),
], ids=[
'open ssh, not tracked',
'open gnupg, not tracked',
'open ssh, tracked',
'open gnupg, tracked',
'missing ssh, tracked',
'missing gnupg, tracked',
])
def test_clone_perms(
runner, yadm_cmd, paths, repo_config,
private_type, in_repo, in_work):
"private_type, in_repo, in_work",
[
("ssh", False, True),
("gnupg", False, True),
("ssh", True, True),
("gnupg", True, True),
("ssh", True, False),
("gnupg", True, False),
],
ids=[
"open ssh, not tracked",
"open gnupg, not tracked",
"open ssh, tracked",
"open gnupg, tracked",
"missing ssh, tracked",
"missing gnupg, tracked",
],
)
def test_clone_perms(runner, yadm_cmd, paths, repo_config, private_type, in_repo, in_work):
"""Test clone permission-related functions"""
# update remote repo to include private data
if in_repo:
rpath = paths.work.mkdir(f'.{private_type}').join('related')
rpath.write('related')
rpath = paths.work.mkdir(f".{private_type}").join("related")
rpath.write("related")
os.system(f'GIT_DIR="{paths.remote}" git add {rpath}')
os.system(f'GIT_DIR="{paths.remote}" git commit -m "{rpath}"')
rpath.remove()
# ensure local private data is insecure at the start
if in_work:
pdir = paths.work.join(f'.{private_type}')
pdir = paths.work.join(f".{private_type}")
if not pdir.exists():
pdir.mkdir()
pfile = pdir.join('existing')
pfile.write('existing')
pfile = pdir.join("existing")
pfile.write("existing")
pdir.chmod(0o777)
pfile.chmod(0o777)
else:
paths.work.remove()
paths.work.mkdir()
env = {'HOME': paths.work}
run = runner(
yadm_cmd('clone', '-d', '-w', paths.work, f'file://{paths.remote}'),
env=env
)
env = {"HOME": paths.work}
run = runner(yadm_cmd("clone", "-d", "-w", paths.work, f"file://{paths.remote}"), env=env)
assert successful_clone(run, paths, repo_config)
verify_head(paths, 'master')
verify_head(paths, "master")
if in_work:
# private directories which already exist, should be left as they are,
# which in this test is "insecure".
assert re.search(
f'initial private dir perms drwxrwxrwx.+.{private_type}',
run.out)
assert re.search(
f'pre-checkout private dir perms drwxrwxrwx.+.{private_type}',
run.out)
assert re.search(
f'post-checkout private dir perms drwxrwxrwx.+.{private_type}',
run.out)
assert re.search(f"initial private dir perms drwxrwxrwx.+.{private_type}", run.out)
assert re.search(f"pre-checkout private dir perms drwxrwxrwx.+.{private_type}", run.out)
assert re.search(f"post-checkout private dir perms drwxrwxrwx.+.{private_type}", run.out)
else:
# private directories which are created, should be done prior to
# checkout, and with secure permissions.
assert 'initial private dir perms' not in run.out
assert re.search(
f'pre-checkout private dir perms drwx------.+.{private_type}',
run.out)
assert re.search(
f'post-checkout private dir perms drwx------.+.{private_type}',
run.out)
assert "initial private dir perms" not in run.out
assert re.search(f"pre-checkout private dir perms drwx------.+.{private_type}", run.out)
assert re.search(f"post-checkout private dir perms drwx------.+.{private_type}", run.out)
# standard perms still apply afterwards unless disabled with auto.perms
assert oct(
paths.work.join(f'.{private_type}').stat().mode).endswith('00'), (
f'.{private_type} has not been secured by auto.perms')
assert oct(paths.work.join(f".{private_type}").stat().mode).endswith(
"00"
), f".{private_type} has not been secured by auto.perms"
@pytest.mark.usefixtures('remote')
@pytest.mark.parametrize(
'branch', ['master', 'default', 'valid', 'invalid'])
@pytest.mark.usefixtures("remote")
@pytest.mark.parametrize("branch", ["master", "default", "valid", "invalid"])
def test_alternate_branch(runner, paths, yadm_cmd, repo_config, branch):
"""Test cloning a branch other than master"""
# add a "valid" branch to the remote
os.system(f'GIT_DIR="{paths.remote}" git checkout -b valid')
os.system(
f'GIT_DIR="{paths.remote}" git commit '
f'--allow-empty -m "This branch is valid"')
if branch != 'default':
os.system(f'GIT_DIR="{paths.remote}" git commit ' f'--allow-empty -m "This branch is valid"')
if branch != "default":
# When branch == 'default', the "default" branch of the remote repo
# will remain "valid" to validate identification the correct default
# branch by inspecting the repo. Otherwise it will be set back to
@ -278,45 +293,43 @@ def test_alternate_branch(runner, paths, yadm_cmd, repo_config, branch):
paths.work.remove()
paths.work.mkdir()
remote_url = f'file://{paths.remote}'
remote_url = f"file://{paths.remote}"
# run the clone command
args = ['clone', '-w', paths.work]
if branch not in ['master', 'default']:
args += ['-b', branch]
args = ["clone", "-w", paths.work]
if branch not in ["master", "default"]:
args += ["-b", branch]
args += [remote_url]
run = runner(command=yadm_cmd(*args))
if branch == 'invalid':
if branch == "invalid":
assert run.failure
assert 'ERROR: Unable to clone the repository' in run.err
assert "ERROR: Unable to clone the repository" in run.err
assert f"Remote branch {branch} not found in upstream" in run.err
else:
assert successful_clone(run, paths, repo_config)
# confirm correct Git origin
run = runner(
command=('git', 'remote', '-v', 'show'),
env={'GIT_DIR': paths.repo})
run = runner(command=("git", "remote", "-v", "show"), env={"GIT_DIR": paths.repo})
assert run.success
assert run.err == ''
assert f'origin\t{remote_url}' in run.out
run = runner(command=yadm_cmd('show'))
if branch == 'master':
assert 'Initial commit' in run.out
verify_head(paths, 'master')
assert run.err == ""
assert f"origin\t{remote_url}" in run.out
run = runner(command=yadm_cmd("show"))
if branch == "master":
assert "Initial commit" in run.out
verify_head(paths, "master")
else:
assert 'This branch is valid' in run.out
verify_head(paths, 'valid')
assert "This branch is valid" in run.out
verify_head(paths, "valid")
def successful_clone(run, paths, repo_config, expected_code=0):
"""Assert clone is successful"""
assert run.code == expected_code
assert oct(paths.repo.stat().mode).endswith('00'), 'Repo is not secured'
assert repo_config('core.bare') == 'false'
assert repo_config('status.showUntrackedFiles') == 'no'
assert repo_config('yadm.managed') == 'true'
assert oct(paths.repo.stat().mode).endswith("00"), "Repo is not secured"
assert repo_config("core.bare") == "false"
assert repo_config("status.showUntrackedFiles") == "no"
assert repo_config("yadm.managed") == "true"
return True
@ -325,21 +338,74 @@ def remote(paths, ds1_repo_copy):
"""Function scoped remote (based on ds1)"""
# pylint: disable=unused-argument
# This is ignored because
# @pytest.mark.usefixtures('ds1_remote_copy')
# @pytest.mark.usefixtures('ds1_repo_copy')
# cannot be applied to another fixture.
paths.remote.remove()
paths.repo.move(paths.remote)
def test_no_repo(runner, yadm_cmd, ):
@pytest.fixture()
def remote_with_submodules(tmpdir_factory, runner, paths, remote, ds1_work_copy):
"""Function scoped remote with submodules (based on ds1)"""
# pylint: disable=unused-argument
# This is ignored because
# @pytest.mark.usefixtures('remote', 'ds1_work_copy')
# cannot be applied to another fixture.
submodule = tmpdir_factory.mktemp("submodule")
paths.remote.copy(submodule)
env = os.environ.copy()
env["GIT_DIR"] = str(paths.remote)
for path in ("a", "b", "d1/c"):
run = runner(
["git", "-C", paths.work, "-c", "protocol.file.allow=always", "submodule", "add", submodule, path],
env=env,
report=False,
)
assert run.success
run = runner(["git", "-C", paths.work, "commit", "-m", '"Add submodules"'], env=env, report=False)
assert run.success
def test_no_repo(runner, yadm_cmd):
"""Test cloning without specifying a repo"""
run = runner(command=yadm_cmd('clone', '-f'))
run = runner(command=yadm_cmd("clone", "-f"))
assert run.failure
assert run.out == ''
assert 'ERROR: Unable to clone the repository' in run.err
assert 'repository \'repo.git\' does not exist' in run.err
assert run.out == ""
assert "ERROR: Unable to clone the repository" in run.err
assert "repository 'repo.git' does not exist" in run.err
def verify_head(paths, branch):
"""Assert the local repo has the correct head branch"""
assert paths.repo.join('HEAD').read() == f'ref: refs/heads/{branch}\n'
assert paths.repo.join("HEAD").read() == f"ref: refs/heads/{branch}\n"
@pytest.mark.usefixtures("remote")
def test_clone_subdirectory(runner, paths, yadm_cmd, repo_config):
"""Test clone from sub-directory of YADM_WORK"""
# clear out the work path
paths.work.remove()
paths.work.mkdir()
# create sub-directory
subdir = paths.work.mkdir("subdir")
# determine remote url
remote_url = f"file://{paths.remote}"
# run the clone command
args = ["clone", "-w", paths.work, remote_url]
run = runner(command=yadm_cmd(*args), cwd=subdir)
# clone should succeed, and repo should be configured properly
assert successful_clone(run, paths, repo_config)
# ensure that no changes found as this is a clean dotfiles clone
run = runner(command=yadm_cmd("status", "-uno", "--porcelain"), cwd=subdir)
assert run.success
assert run.out == ""
assert run.err == ""

View file

@ -1,13 +1,14 @@
"""Test config"""
import os
import pytest
TEST_SECTION = 'test'
TEST_ATTRIBUTE = 'attribute'
TEST_KEY = f'{TEST_SECTION}.{TEST_ATTRIBUTE}'
TEST_VALUE = 'testvalue'
TEST_FILE = f'[{TEST_SECTION}]\n\t{TEST_ATTRIBUTE} = {TEST_VALUE}'
TEST_SECTION = "test"
TEST_ATTRIBUTE = "attribute"
TEST_KEY = f"{TEST_SECTION}.{TEST_ATTRIBUTE}"
TEST_VALUE = "testvalue"
TEST_FILE = f"[{TEST_SECTION}]\n\t{TEST_ATTRIBUTE} = {TEST_VALUE}"
def test_config_no_params(runner, yadm_cmd, supported_configs):
@ -18,11 +19,11 @@ def test_config_no_params(runner, yadm_cmd, supported_configs):
Exit with 0
"""
run = runner(yadm_cmd('config'))
run = runner(yadm_cmd("config"))
assert run.success
assert run.err == ''
assert 'Please read the CONFIGURATION section' in run.out
assert run.err == ""
assert "Please read the CONFIGURATION section" in run.out
for config in supported_configs:
assert config in run.out
@ -34,11 +35,11 @@ def test_config_read_missing(runner, yadm_cmd):
Exit with 0
"""
run = runner(yadm_cmd('config', TEST_KEY))
run = runner(yadm_cmd("config", TEST_KEY))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
def test_config_write(runner, yadm_cmd, paths):
@ -49,11 +50,11 @@ def test_config_write(runner, yadm_cmd, paths):
Exit with 0
"""
run = runner(yadm_cmd('config', TEST_KEY, TEST_VALUE))
run = runner(yadm_cmd("config", TEST_KEY, TEST_VALUE))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
assert paths.config.read().strip() == TEST_FILE
@ -65,10 +66,10 @@ def test_config_read(runner, yadm_cmd, paths):
"""
paths.config.write(TEST_FILE)
run = runner(yadm_cmd('config', TEST_KEY))
run = runner(yadm_cmd("config", TEST_KEY))
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out.strip() == TEST_VALUE
@ -82,16 +83,16 @@ def test_config_update(runner, yadm_cmd, paths):
paths.config.write(TEST_FILE)
run = runner(yadm_cmd('config', TEST_KEY, TEST_VALUE + 'extra'))
run = runner(yadm_cmd("config", TEST_KEY, TEST_VALUE + "extra"))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
assert paths.config.read().strip() == TEST_FILE + 'extra'
assert paths.config.read().strip() == TEST_FILE + "extra"
@pytest.mark.usefixtures('ds1_repo_copy')
@pytest.mark.usefixtures("ds1_repo_copy")
def test_config_local_read(runner, yadm_cmd, paths, supported_local_configs):
"""Read local attribute
@ -101,19 +102,17 @@ def test_config_local_read(runner, yadm_cmd, paths, supported_local_configs):
# populate test values
for config in supported_local_configs:
os.system(
f'GIT_DIR="{paths.repo}" '
f'git config --local "{config}" "value_of_{config}"')
os.system(f'GIT_DIR="{paths.repo}" ' f'git config --local "{config}" "value_of_{config}"')
# run yadm config
for config in supported_local_configs:
run = runner(yadm_cmd('config', config))
run = runner(yadm_cmd("config", config))
assert run.success
assert run.err == ''
assert run.out.strip() == f'value_of_{config}'
assert run.err == ""
assert run.out.strip() == f"value_of_{config}"
@pytest.mark.usefixtures('ds1_repo_copy')
@pytest.mark.usefixtures("ds1_repo_copy")
def test_config_local_write(runner, yadm_cmd, paths, supported_local_configs):
"""Write local attribute
@ -124,40 +123,37 @@ def test_config_local_write(runner, yadm_cmd, paths, supported_local_configs):
# run yadm config
for config in supported_local_configs:
run = runner(yadm_cmd('config', config, f'value_of_{config}'))
run = runner(yadm_cmd("config", config, f"value_of_{config}"))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
# verify test values
for config in supported_local_configs:
run = runner(
command=('git', 'config', config),
env={'GIT_DIR': paths.repo})
run = runner(command=("git", "config", config), env={"GIT_DIR": paths.repo})
assert run.success
assert run.err == ''
assert run.out.strip() == f'value_of_{config}'
assert run.err == ""
assert run.out.strip() == f"value_of_{config}"
def test_config_without_parent_directory(runner, yadm_cmd, paths):
"""Write and read attribute to/from config file with non-existent parent dir
"""Write/read attribute to/from config file with non-existent parent dir
Update configuration file
Display value
Exit with 0
"""
config_file = paths.root + '/folder/does/not/exist/config'
config_file = paths.root + "/folder/does/not/exist/config"
run = runner(
yadm_cmd('--yadm-config', config_file, 'config', TEST_KEY, TEST_VALUE))
run = runner(yadm_cmd("--yadm-config", config_file, "config", TEST_KEY, TEST_VALUE))
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
run = runner(yadm_cmd('--yadm-config', config_file, 'config', TEST_KEY))
run = runner(yadm_cmd("--yadm-config", config_file, "config", TEST_KEY))
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out.strip() == TEST_VALUE

View file

@ -1,30 +1,30 @@
"""Test encryption"""
import os
import pipes
import time
import shlex
import pytest
KEY_FILE = 'test/test_key'
KEY_FINGERPRINT = 'F8BBFC746C58945442349BCEBA54FFD04C599B1A'
KEY_NAME = 'yadm-test1'
KEY_TRUST = 'test/ownertrust.txt'
PASSPHRASE = 'ExamplePassword'
KEY_FILE = "test/test_key"
KEY_FINGERPRINT = "F8BBFC746C58945442349BCEBA54FFD04C599B1A"
KEY_NAME = "yadm-test1"
KEY_TRUST = "test/ownertrust.txt"
PASSPHRASE = "ExamplePassword"
pytestmark = pytest.mark.usefixtures('config_git')
pytestmark = pytest.mark.usefixtures("config_git")
def add_asymmetric_key(runner, gnupg):
"""Add asymmetric key"""
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
runner(
['gpg', '--import', pipes.quote(KEY_FILE)],
["gpg", "--import", shlex.quote(KEY_FILE)],
env=env,
shell=True,
)
runner(
['gpg', '--import-ownertrust', '<', pipes.quote(KEY_TRUST)],
["gpg", "--import-ownertrust", "<", shlex.quote(KEY_TRUST)],
env=env,
shell=True,
)
@ -33,20 +33,14 @@ def add_asymmetric_key(runner, gnupg):
def remove_asymmetric_key(runner, gnupg):
"""Remove asymmetric key"""
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
runner(
[
'gpg', '--batch', '--yes',
'--delete-secret-keys', pipes.quote(KEY_FINGERPRINT)
],
["gpg", "--batch", "--yes", "--delete-secret-keys", shlex.quote(KEY_FINGERPRINT)],
env=env,
shell=True,
)
runner(
[
'gpg', '--batch', '--yes',
'--delete-key', pipes.quote(KEY_FINGERPRINT)
],
["gpg", "--batch", "--yes", "--delete-key", shlex.quote(KEY_FINGERPRINT)],
env=env,
shell=True,
)
@ -78,48 +72,49 @@ def encrypt_targets(yadm_cmd, paths):
"""
# init empty yadm repo
os.system(' '.join(yadm_cmd('init', '-w', str(paths.work), '-f')))
os.system(" ".join(yadm_cmd("init", "-w", str(paths.work), "-f")))
expected = []
# standard files w/ dirs & spaces
paths.work.join('inc file1').write('inc file1')
expected.append('inc file1')
paths.encrypt.write('inc file1\n')
paths.work.join('inc dir').mkdir()
paths.work.join('inc dir/inc file2').write('inc file2')
expected.append('inc dir/inc file2')
paths.encrypt.write('inc dir/inc file2\n', mode='a')
paths.work.join("inc file1").write("inc file1")
expected.append("inc file1")
paths.encrypt.write("inc file1\n")
paths.work.join("inc dir").mkdir()
paths.work.join("inc dir/inc file2").write("inc file2")
expected.append("inc dir/inc file2")
paths.encrypt.write("inc dir/inc file2\n", mode="a")
# standard globs w/ dirs & spaces
paths.work.join('globs file1').write('globs file1')
expected.append('globs file1')
paths.work.join('globs dir').mkdir()
paths.work.join('globs dir/globs file2').write('globs file2')
expected.append('globs dir/globs file2')
paths.encrypt.write('globs*\n', mode='a')
paths.work.join("globs file1").write("globs file1")
expected.append("globs file1")
paths.work.join("globs dir").mkdir()
paths.work.join("globs dir/globs file2").write("globs file2")
expected.append("globs dir/globs file2")
paths.encrypt.write("globs*\n", mode="a")
paths.encrypt.write("globs d*/globs*\n", mode="a")
# blank lines
paths.encrypt.write('\n \n\t\n', mode='a')
paths.encrypt.write("\n \n\t\n", mode="a")
# comments
paths.work.join('commentfile1').write('commentfile1')
paths.encrypt.write('#commentfile1\n', mode='a')
paths.encrypt.write(' #commentfile1\n', mode='a')
paths.work.join("commentfile1").write("commentfile1")
paths.encrypt.write("#commentfile1\n", mode="a")
paths.encrypt.write(" #commentfile1\n", mode="a")
# exclusions
paths.work.join('extest').mkdir()
paths.encrypt.write('extest/*\n', mode='a') # include within extest
paths.work.join('extest/inglob1').write('inglob1')
paths.work.join('extest/exglob1').write('exglob1')
paths.work.join('extest/exglob2').write('exglob2')
paths.encrypt.write('!extest/ex*\n', mode='a') # exclude the ex*
expected.append('extest/inglob1') # should be left with only in*
paths.work.join("extest").mkdir()
paths.encrypt.write("extest/*\n", mode="a") # include within extest
paths.work.join("extest/inglob1").write("inglob1")
paths.work.join("extest/exglob1").write("exglob1")
paths.work.join("extest/exglob2").write("exglob2")
paths.encrypt.write("!extest/ex*\n", mode="a") # exclude the ex*
expected.append("extest/inglob1") # should be left with only in*
return expected
@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def decrypt_targets(tmpdir_factory, runner, gnupg):
"""Fixture for setting data to decrypt
@ -128,234 +123,202 @@ def decrypt_targets(tmpdir_factory, runner, gnupg):
* creates a list of expected decrypted files
"""
tmpdir = tmpdir_factory.mktemp('decrypt_targets')
symmetric = tmpdir.join('symmetric.tar.gz.gpg')
asymmetric = tmpdir.join('asymmetric.tar.gz.gpg')
tmpdir = tmpdir_factory.mktemp("decrypt_targets")
symmetric = tmpdir.join("symmetric.tar.gz.gpg")
asymmetric = tmpdir.join("asymmetric.tar.gz.gpg")
expected = []
tmpdir.join('decrypt1').write('decrypt1')
expected.append('decrypt1')
tmpdir.join('decrypt2').write('decrypt2')
expected.append('decrypt2')
tmpdir.join('subdir').mkdir()
tmpdir.join('subdir/decrypt3').write('subdir/decrypt3')
expected.append('subdir/decrypt3')
tmpdir.join("decrypt1").write("decrypt1")
expected.append("decrypt1")
tmpdir.join("decrypt2").write("decrypt2")
expected.append("decrypt2")
tmpdir.join("subdir").mkdir()
tmpdir.join("subdir/decrypt3").write("subdir/decrypt3")
expected.append("subdir/decrypt3")
gnupg.pw(PASSPHRASE)
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
run = runner(
['tar', 'cvf', '-'] +
expected +
['|', 'gpg', '--batch', '--yes', '-c'] +
['--output', pipes.quote(str(symmetric))],
["tar", "cvf", "-"]
+ expected
+ ["|", "gpg", "--batch", "--yes", "-c"]
+ ["--output", shlex.quote(str(symmetric))],
cwd=tmpdir,
env=env,
shell=True)
shell=True,
)
assert run.success
gnupg.pw('')
gnupg.pw("")
add_asymmetric_key(runner, gnupg)
run = runner(
['tar', 'cvf', '-'] +
expected +
['|', 'gpg', '--batch', '--yes', '-e'] +
['-r', pipes.quote(KEY_NAME)] +
['--output', pipes.quote(str(asymmetric))],
["tar", "cvf", "-"]
+ expected
+ ["|", "gpg", "--batch", "--yes", "-e"]
+ ["-r", shlex.quote(KEY_NAME)]
+ ["--output", shlex.quote(str(asymmetric))],
cwd=tmpdir,
env=env,
shell=True)
shell=True,
)
assert run.success
remove_asymmetric_key(runner, gnupg)
return {
'asymmetric': asymmetric,
'expected': expected,
'symmetric': symmetric,
"asymmetric": asymmetric,
"expected": expected,
"symmetric": symmetric,
}
@pytest.mark.parametrize(
'bad_phrase', [False, True],
ids=['good_phrase', 'bad_phrase'])
@pytest.mark.parametrize(
'missing_encrypt', [False, True],
ids=['encrypt_exists', 'encrypt_missing'])
@pytest.mark.parametrize(
'overwrite', [False, True],
ids=['clean', 'overwrite'])
def test_symmetric_encrypt(
runner, yadm_cmd, paths, encrypt_targets,
gnupg, bad_phrase, overwrite, missing_encrypt):
@pytest.mark.parametrize("bad_phrase", [False, True], ids=["good_phrase", "bad_phrase"])
@pytest.mark.parametrize("missing_encrypt", [False, True], ids=["encrypt_exists", "encrypt_missing"])
@pytest.mark.parametrize("overwrite", [False, True], ids=["clean", "overwrite"])
def test_symmetric_encrypt(runner, yadm_cmd, paths, encrypt_targets, gnupg, bad_phrase, overwrite, missing_encrypt):
"""Test symmetric encryption"""
if missing_encrypt:
paths.encrypt.remove()
if bad_phrase:
gnupg.pw('')
gnupg.pw("")
else:
gnupg.pw(PASSPHRASE)
if overwrite:
paths.archive.write('existing archive')
paths.archive.write("existing archive")
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
run = runner(yadm_cmd('encrypt'), env=env)
env["GNUPGHOME"] = gnupg.home
run = runner(yadm_cmd("encrypt"), env=env)
if missing_encrypt or bad_phrase:
assert run.failure
else:
assert run.success
assert run.err == ''
assert run.err == ""
if missing_encrypt:
assert 'does not exist' in run.err
assert "does not exist" in run.err
elif bad_phrase:
assert 'Invalid passphrase' in run.err
assert "Invalid IPC" in run.err
else:
assert encrypted_data_valid(
runner, gnupg, paths.archive, encrypt_targets)
assert encrypted_data_valid(runner, gnupg, paths.archive, encrypt_targets)
@pytest.mark.parametrize(
'bad_phrase', [False, True],
ids=['good_phrase', 'bad_phrase'])
@pytest.mark.parametrize(
'archive_exists', [True, False],
ids=['archive_exists', 'archive_missing'])
@pytest.mark.parametrize(
'dolist', [False, True],
ids=['decrypt', 'list'])
def test_symmetric_decrypt(
runner, yadm_cmd, paths, decrypt_targets, gnupg,
dolist, archive_exists, bad_phrase):
@pytest.mark.parametrize("bad_phrase", [False, True], ids=["good_phrase", "bad_phrase"])
@pytest.mark.parametrize("archive_exists", [True, False], ids=["archive_exists", "archive_missing"])
@pytest.mark.parametrize("dolist", [False, True], ids=["decrypt", "list"])
def test_symmetric_decrypt(runner, yadm_cmd, paths, decrypt_targets, gnupg, dolist, archive_exists, bad_phrase):
"""Test decryption"""
# init empty yadm repo
os.system(' '.join(yadm_cmd('init', '-w', str(paths.work), '-f')))
os.system(" ".join(yadm_cmd("init", "-w", str(paths.work), "-f")))
if bad_phrase:
gnupg.pw('')
time.sleep(1) # allow gpg-agent cache to expire
gnupg.pw("")
else:
gnupg.pw(PASSPHRASE)
if archive_exists:
decrypt_targets['symmetric'].copy(paths.archive)
decrypt_targets["symmetric"].copy(paths.archive)
# to test overwriting
paths.work.join('decrypt1').write('pre-existing file')
paths.work.join("decrypt1").write("pre-existing file")
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
args = []
if dolist:
args.append('-l')
run = runner(yadm_cmd('decrypt') + args, env=env)
args.append("-l")
run = runner(yadm_cmd("decrypt") + args, env=env)
if archive_exists and not bad_phrase:
assert run.success
assert 'encrypted with 1 passphrase' in run.err
assert "encrypted with 1 passphrase" in run.err
if dolist:
for filename in decrypt_targets['expected']:
if filename != 'decrypt1': # this one should exist
for filename in decrypt_targets["expected"]:
if filename != "decrypt1": # this one should exist
assert not paths.work.join(filename).exists()
assert filename in run.out
else:
for filename in decrypt_targets['expected']:
for filename in decrypt_targets["expected"]:
assert paths.work.join(filename).read() == filename
else:
assert run.failure
@pytest.mark.usefixtures('asymmetric_key')
@pytest.mark.parametrize(
'ask', [False, True],
ids=['no_ask', 'ask'])
@pytest.mark.parametrize(
'key_exists', [True, False],
ids=['key_exists', 'key_missing'])
@pytest.mark.parametrize(
'overwrite', [False, True],
ids=['clean', 'overwrite'])
def test_asymmetric_encrypt(
runner, yadm_cmd, paths, encrypt_targets, gnupg,
overwrite, key_exists, ask):
@pytest.mark.usefixtures("asymmetric_key")
@pytest.mark.parametrize("ask", [False, True], ids=["no_ask", "ask"])
@pytest.mark.parametrize("key_exists", [True, False], ids=["key_exists", "key_missing"])
@pytest.mark.parametrize("overwrite", [False, True], ids=["clean", "overwrite"])
def test_asymmetric_encrypt(runner, yadm_cmd, paths, encrypt_targets, gnupg, overwrite, key_exists, ask):
"""Test asymmetric encryption"""
# specify encryption recipient
if ask:
os.system(' '.join(yadm_cmd('config', 'yadm.gpg-recipient', 'ASK')))
expect = [('Enter the user ID', KEY_NAME), ('Enter the user ID', '')]
os.system(" ".join(yadm_cmd("config", "yadm.gpg-recipient", "ASK")))
expect = [("Enter the user ID", KEY_NAME), ("Enter the user ID", "")]
else:
os.system(' '.join(yadm_cmd('config', 'yadm.gpg-recipient', KEY_NAME)))
os.system(" ".join(yadm_cmd("config", "yadm.gpg-recipient", KEY_NAME)))
expect = []
if overwrite:
paths.archive.write('existing archive')
paths.archive.write("existing archive")
if not key_exists:
remove_asymmetric_key(runner, gnupg)
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
run = runner(yadm_cmd('encrypt'), env=env, expect=expect)
run = runner(yadm_cmd("encrypt"), env=env, expect=expect)
if key_exists:
assert run.success
assert encrypted_data_valid(
runner, gnupg, paths.archive, encrypt_targets)
assert encrypted_data_valid(runner, gnupg, paths.archive, encrypt_targets)
else:
assert run.failure
assert 'Unable to write' in run.out if expect else run.err
assert "Unable to write" in run.out if expect else run.err
if ask:
assert 'Enter the user ID' in run.out
assert "Enter the user ID" in run.out
@pytest.mark.usefixtures('asymmetric_key')
@pytest.mark.usefixtures('encrypt_targets')
@pytest.mark.usefixtures("asymmetric_key")
@pytest.mark.usefixtures("encrypt_targets")
def test_multi_key(runner, yadm_cmd, gnupg):
"""Test multiple recipients"""
# specify two encryption recipient
os.system(' '.join(yadm_cmd(
'config', 'yadm.gpg-recipient', f'"second-key {KEY_NAME}"')))
os.system(" ".join(yadm_cmd("config", "yadm.gpg-recipient", f'"second-key {KEY_NAME}"')))
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
run = runner(yadm_cmd('encrypt'), env=env)
run = runner(yadm_cmd("encrypt"), env=env)
assert run.failure
assert 'second-key: skipped: No public key' in run.err
assert "second-key: skipped: No public key" in run.err
@pytest.mark.usefixtures('asymmetric_key')
@pytest.mark.parametrize(
'key_exists', [True, False],
ids=['key_exists', 'key_missing'])
@pytest.mark.parametrize(
'dolist', [False, True],
ids=['decrypt', 'list'])
def test_asymmetric_decrypt(
runner, yadm_cmd, paths, decrypt_targets, gnupg,
dolist, key_exists):
@pytest.mark.usefixtures("asymmetric_key")
@pytest.mark.parametrize("key_exists", [True, False], ids=["key_exists", "key_missing"])
@pytest.mark.parametrize("dolist", [False, True], ids=["decrypt", "list"])
def test_asymmetric_decrypt(runner, yadm_cmd, paths, decrypt_targets, gnupg, dolist, key_exists):
"""Test decryption"""
# init empty yadm repo
os.system(' '.join(yadm_cmd('init', '-w', str(paths.work), '-f')))
os.system(" ".join(yadm_cmd("init", "-w", str(paths.work), "-f")))
decrypt_targets['asymmetric'].copy(paths.archive)
decrypt_targets["asymmetric"].copy(paths.archive)
# to test overwriting
paths.work.join('decrypt1').write('pre-existing file')
paths.work.join("decrypt1").write("pre-existing file")
if not key_exists:
remove_asymmetric_key(runner, gnupg)
@ -363,32 +326,28 @@ def test_asymmetric_decrypt(
args = []
if dolist:
args.append('-l')
args.append("-l")
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
run = runner(yadm_cmd('decrypt') + args, env=env)
env["GNUPGHOME"] = gnupg.home
run = runner(yadm_cmd("decrypt") + args, env=env)
if key_exists:
assert run.success
if dolist:
for filename in decrypt_targets['expected']:
if filename != 'decrypt1': # this one should exist
for filename in decrypt_targets["expected"]:
if filename != "decrypt1": # this one should exist
assert not paths.work.join(filename).exists()
assert filename in run.out
else:
for filename in decrypt_targets['expected']:
for filename in decrypt_targets["expected"]:
assert paths.work.join(filename).read() == filename
else:
assert run.failure
assert 'Unable to extract encrypted files' in run.err
assert "Unable to extract encrypted files" in run.err
@pytest.mark.parametrize(
'untracked',
[False, 'y', 'n'],
ids=['tracked', 'untracked_answer_y', 'untracked_answer_n'])
def test_offer_to_add(
runner, yadm_cmd, paths, encrypt_targets, gnupg, untracked):
@pytest.mark.parametrize("untracked", [False, "y", "n"], ids=["tracked", "untracked_answer_y", "untracked_answer_n"])
def test_offer_to_add(runner, yadm_cmd, paths, encrypt_targets, gnupg, untracked):
"""Test offer to add encrypted archive
All the other encryption tests use an archive outside of the work tree.
@ -396,85 +355,75 @@ def test_offer_to_add(
should be an offer to add it to the repo if it is not tracked.
"""
worktree_archive = paths.work.join('worktree-archive.tar.gpg')
worktree_archive = paths.work.join("worktree-archive.tar.gpg")
expect = []
gnupg.pw(PASSPHRASE)
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
if untracked:
expect.append(('add it now', untracked))
expect.append(("add it now", untracked))
else:
worktree_archive.write('exists')
os.system(' '.join(yadm_cmd('add', str(worktree_archive))))
worktree_archive.write("exists")
os.system(" ".join(yadm_cmd("add", str(worktree_archive))))
run = runner(
yadm_cmd('encrypt', '--yadm-archive', str(worktree_archive)),
env=env,
expect=expect
)
run = runner(yadm_cmd("encrypt", "--yadm-archive", str(worktree_archive)), env=env, expect=expect)
assert run.success
assert run.err == ''
assert encrypted_data_valid(
runner, gnupg, worktree_archive, encrypt_targets)
assert run.err == ""
assert encrypted_data_valid(runner, gnupg, worktree_archive, encrypt_targets)
run = runner(
yadm_cmd('status', '--porcelain', '-uall', str(worktree_archive)))
run = runner(yadm_cmd("status", "--porcelain", "-uall", str(worktree_archive)))
assert run.success
assert run.err == ''
assert run.err == ""
if untracked == 'y':
if untracked == "y":
# should be added to the index
assert f'A {worktree_archive.basename}' in run.out
elif untracked == 'n':
assert f"A {worktree_archive.basename}" in run.out
elif untracked == "n":
# should NOT be added to the index
assert f'?? {worktree_archive.basename}' in run.out
assert f"?? {worktree_archive.basename}" in run.out
else:
# should appear modified in the index
assert f'AM {worktree_archive.basename}' in run.out
assert f"AM {worktree_archive.basename}" in run.out
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.usefixtures("ds1_copy")
def test_encrypt_added_to_exclude(runner, yadm_cmd, paths, gnupg):
"""Confirm that .config/yadm/encrypt is added to exclude"""
gnupg.pw(PASSPHRASE)
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
env["GNUPGHOME"] = gnupg.home
exclude_file = paths.repo.join('info/exclude')
paths.encrypt.write('test-encrypt-data\n')
paths.work.join('test-encrypt-data').write('')
exclude_file.write('original-data', ensure=True)
exclude_file = paths.repo.join("info/exclude")
paths.encrypt.write("test-encrypt-data\n")
paths.work.join("test-encrypt-data").write("")
exclude_file.write("original-data", ensure=True)
run = runner(yadm_cmd('encrypt'), env=env)
run = runner(yadm_cmd("encrypt"), env=env)
assert 'test-encrypt-data' in paths.repo.join('info/exclude').read()
assert 'original-data' in paths.repo.join('info/exclude').read()
assert "test-encrypt-data" in exclude_file.read()
assert "original-data" in exclude_file.read()
assert run.success
assert run.err == ''
assert run.err == ""
def encrypted_data_valid(runner, gnupg, encrypted, expected):
"""Verify encrypted data matches expectations"""
gnupg.pw(PASSPHRASE)
env = os.environ.copy()
env['GNUPGHOME'] = gnupg.home
run = runner([
'gpg',
'-d', pipes.quote(str(encrypted)),
'2>/dev/null',
'|', 'tar', 't'], env=env, shell=True, report=False)
env["GNUPGHOME"] = gnupg.home
run = runner(
["gpg", "-d", shlex.quote(str(encrypted)), "2>/dev/null", "|", "tar", "t"], env=env, shell=True, report=False
)
file_count = 0
for filename in run.out.splitlines():
if filename.endswith('/'):
if filename.endswith("/"):
continue
file_count += 1
assert filename in expected, (
f'Unexpected file in archive: {filename}')
assert file_count == len(expected), (
'Number of files in archive does not match expected')
assert filename in expected, f"Unexpected file in archive: {filename}"
assert file_count == len(expected), "Number of files in archive does not match expected"
return True

View file

@ -1,110 +1,117 @@
"""Test enter"""
import os
import pytest
@pytest.mark.parametrize(
'shell, success', [
('delete', True), # if there is no shell variable, bash creates it
('', False),
('/usr/bin/env', True),
('noexec', False),
], ids=[
'shell-missing',
'shell-empty',
'shell-env',
'shell-noexec',
])
@pytest.mark.usefixtures('ds1_copy')
"shell, success",
[
("delete", True), # if there is no shell variable, bash creates it
("", False),
("/usr/bin/env", True),
("noexec", False),
],
ids=[
"shell-missing",
"shell-empty",
"shell-env",
"shell-noexec",
],
)
@pytest.mark.usefixtures("ds1_copy")
def test_enter(runner, yadm_cmd, paths, shell, success):
"""Enter tests"""
env = os.environ.copy()
if shell == 'delete':
if shell == "delete":
# remove shell
if 'SHELL' in env:
del env['SHELL']
elif shell == 'noexec':
if "SHELL" in env:
del env["SHELL"]
elif shell == "noexec":
# specify a non-executable path
noexec = paths.root.join('noexec')
noexec.write('')
noexec = paths.root.join("noexec")
noexec.write("")
noexec.chmod(0o664)
env['SHELL'] = str(noexec)
env["SHELL"] = str(noexec)
else:
env['SHELL'] = shell
env["SHELL"] = shell
run = runner(command=yadm_cmd('enter'), env=env)
run = runner(command=yadm_cmd("enter"), env=env)
assert run.success == success
prompt = f'yadm shell ({paths.repo})'
prompt = f"yadm shell ({paths.repo})"
if success:
assert run.out.startswith('Entering yadm repo')
assert run.out.rstrip().endswith('Leaving yadm repo')
assert run.err == ''
assert run.out.startswith("Entering yadm repo")
assert run.out.rstrip().endswith("Leaving yadm repo")
assert run.err == ""
else:
assert 'does not refer to an executable' in run.err
if 'env' in shell:
assert f'GIT_DIR={paths.repo}' in run.out
assert f'GIT_WORK_TREE={paths.work}' in run.out
assert f'PROMPT={prompt}' in run.out
assert f'PS1={prompt}' in run.out
assert "does not refer to an executable" in run.err
if "env" in shell:
assert f"GIT_DIR={paths.repo}" in run.out
assert f"GIT_WORK_TREE={paths.work}" in run.out
assert f"PROMPT={prompt}" in run.out
assert f"PS1={prompt}" in run.out
@pytest.mark.parametrize(
'shell, opts, path', [
('bash', '--norc', '\\w'),
('csh', '-f', '%~'),
('zsh', '-f', '%~'),
], ids=[
'bash',
'csh',
'zsh',
])
"shell, opts, path",
[
("bash", "--norc", "\\w"),
("csh", "-f", "%~"),
("zsh", "-f", "%~"),
],
ids=[
"bash",
"csh",
"zsh",
],
)
@pytest.mark.parametrize(
'cmd',
[False, 'cmd', 'cmd-bad-exit'],
ids=['no-cmd', 'cmd', 'cmd-bad-exit'])
"cmd",
[False, "cmd", "cmd-bad-exit"],
ids=["no-cmd", "cmd", "cmd-bad-exit"],
)
@pytest.mark.parametrize(
'term', ['', 'dumb'],
ids=['term-empty', 'term-dumb'])
@pytest.mark.usefixtures('ds1_copy')
def test_enter_shell_ops(runner, yadm_cmd, paths, shell,
opts, path, cmd, term):
"term",
["", "dumb"],
ids=["term-empty", "term-dumb"],
)
@pytest.mark.usefixtures("ds1_copy")
def test_enter_shell_ops(runner, yadm_cmd, paths, shell, opts, path, cmd, term):
"""Enter tests for specific shell options"""
change_exit = '\nfalse' if cmd == 'cmd-bad-exit' else ''
change_exit = "\nfalse" if cmd == "cmd-bad-exit" else ""
# Create custom shell to detect options passed
custom_shell = paths.root.join(shell)
custom_shell.write(
f'#!/bin/sh\necho OPTS=$*\necho PROMPT=$PROMPT{change_exit}'
)
custom_shell.write(f"#!/bin/sh\necho OPTS=$*\necho PROMPT=$PROMPT{change_exit}")
custom_shell.chmod(0o775)
test_cmd = ['test1', 'test2', 'test3']
test_cmd = ["test1", "test2", "test3"]
enter_cmd = ['enter']
enter_cmd = ["enter"]
if cmd:
enter_cmd += test_cmd
env = os.environ.copy()
env['TERM'] = term
env['SHELL'] = custom_shell
env["TERM"] = term
env["SHELL"] = custom_shell
if shell == 'zsh' and term == 'dumb':
opts += ' --no-zle'
if shell == "zsh" and term == "dumb":
opts += " --no-zle"
run = runner(command=yadm_cmd(*enter_cmd), env=env)
if cmd == 'cmd-bad-exit':
if cmd == "cmd-bad-exit":
assert run.failure
else:
assert run.success
assert run.err == ''
assert f'OPTS={opts}' in run.out
assert f'PROMPT=yadm shell ({paths.repo}) {path} >' in run.out
assert run.err == ""
assert f"OPTS={opts}" in run.out
assert f"PROMPT=yadm shell ({paths.repo}) {path} >" in run.out
if cmd:
assert '-c ' + ' '.join(test_cmd) in run.out
assert 'Entering yadm repo' not in run.out
assert 'Leaving yadm repo' not in run.out
assert "-c " + " ".join(test_cmd) in run.out
assert "Entering yadm repo" not in run.out
assert "Leaving yadm repo" not in run.out
else:
assert 'Entering yadm repo' in run.out
assert 'Leaving yadm repo' in run.out
assert "Entering yadm repo" in run.out
assert "Leaving yadm repo" in run.out

View file

@ -4,28 +4,30 @@ import pytest
@pytest.mark.parametrize(
'crypt',
[False, 'installed', 'installed-but-failed'],
ids=['not-installed', 'installed', 'installed-but-failed']
"crypt",
[False, "installed", "installed-but-failed"],
ids=["not-installed", "installed", "installed-but-failed"],
)
@pytest.mark.parametrize(
'cmd,var', [
['git_crypt', 'GIT_CRYPT_PROGRAM'],
['transcrypt', 'TRANSCRYPT_PROGRAM'],
],
ids=['git-crypt', 'transcrypt'])
"cmd,var",
[
["git_crypt", "GIT_CRYPT_PROGRAM"],
["transcrypt", "TRANSCRYPT_PROGRAM"],
],
ids=["git-crypt", "transcrypt"],
)
def test_ext_encryption(runner, yadm, paths, tmpdir, crypt, cmd, var):
"""External encryption tests"""
paths.repo.ensure(dir=True)
bindir = tmpdir.mkdir('bin')
pgm = bindir.join('test-ext-crypt')
bindir = tmpdir.mkdir("bin")
pgm = bindir.join("test-ext-crypt")
if crypt:
pgm.write('#!/bin/sh\necho ext-crypt ran\n')
pgm.write("#!/bin/sh\necho ext-crypt ran\n")
pgm.chmod(0o775)
if crypt == 'installed-but-failed':
pgm.write('false\n', mode='a')
if crypt == "installed-but-failed":
pgm.write("false\n", mode="a")
script = f"""
YADM_TEST=1 source {yadm}
@ -34,15 +36,15 @@ def test_ext_encryption(runner, yadm, paths, tmpdir, crypt, cmd, var):
{cmd} "param1"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
if crypt:
if crypt == 'installed-but-failed':
if crypt == "installed-but-failed":
assert run.failure
else:
assert run.success
assert run.out.strip() == 'ext-crypt ran'
assert run.err == ''
assert run.out.strip() == "ext-crypt ran"
assert run.err == ""
else:
assert run.failure
assert f"command '{pgm}' cannot be located" in run.err

View file

@ -1,10 +1,11 @@
"""Test git"""
import re
import pytest
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.usefixtures("ds1_copy")
def test_git(runner, yadm_cmd, paths):
"""Test series of passthrough git commands
@ -17,42 +18,42 @@ def test_git(runner, yadm_cmd, paths):
"""
# passthru unknown commands to Git
run = runner(command=yadm_cmd('bogus'))
run = runner(command=yadm_cmd("bogus"))
assert run.failure
assert "git: 'bogus' is not a git command." in run.err
assert "See 'git --help'" in run.err
assert run.out == ''
assert run.out == ""
# git command 'add' - badfile
run = runner(command=yadm_cmd('add', '-v', 'does_not_exist'))
run = runner(command=yadm_cmd("add", "-v", "does_not_exist"))
assert run.code == 128
assert "pathspec 'does_not_exist' did not match any files" in run.err
assert run.out == ''
assert run.out == ""
# git command 'add'
newfile = paths.work.join('test_git')
newfile.write('test_git')
run = runner(command=yadm_cmd('add', '-v', str(newfile)))
newfile = paths.work.join("test_git")
newfile.write("test_git")
run = runner(command=yadm_cmd("add", "-v", str(newfile)))
assert run.success
assert run.err == ''
assert run.err == ""
assert "add 'test_git'" in run.out
# git command 'status'
run = runner(command=yadm_cmd('status'))
run = runner(command=yadm_cmd("status"))
assert run.success
assert run.err == ''
assert re.search(r'new file:\s+test_git', run.out)
assert run.err == ""
assert re.search(r"new file:\s+test_git", run.out)
# git command 'commit'
run = runner(command=yadm_cmd('commit', '-m', 'Add test_git'))
run = runner(command=yadm_cmd("commit", "-m", "Add test_git"))
assert run.success
assert run.err == ''
assert '1 file changed' in run.out
assert '1 insertion' in run.out
assert re.search(r'create mode .+ test_git', run.out)
assert run.err == ""
assert "1 file changed" in run.out
assert "1 insertion" in run.out
assert re.search(r"create mode .+ test_git", run.out)
# git command 'log'
run = runner(command=yadm_cmd('log', '--oneline'))
run = runner(command=yadm_cmd("log", "--oneline"))
assert run.success
assert run.err == ''
assert 'Add test_git' in run.out
assert run.err == ""
assert "Add test_git" in run.out

View file

@ -1,4 +1,5 @@
"""Test help"""
import pytest
@ -6,14 +7,14 @@ def test_missing_command(runner, yadm_cmd):
"""Run without any command"""
run = runner(command=yadm_cmd())
assert run.failure
assert run.err == ''
assert run.out.startswith('Usage: yadm')
assert run.err == ""
assert run.out.startswith("Usage: yadm")
@pytest.mark.parametrize('cmd', ['--help', 'help'])
@pytest.mark.parametrize("cmd", ["--help", "help"])
def test_help_command(runner, yadm_cmd, cmd):
"""Run with help command"""
run = runner(command=yadm_cmd(cmd))
assert run.failure
assert run.err == ''
assert run.out.startswith('Usage: yadm')
assert run.err == ""
assert run.out.startswith("Usage: yadm")

View file

@ -4,7 +4,8 @@ import pytest
@pytest.mark.parametrize(
'pre, pre_code, post, post_code', [
"pre, pre_code, post, post_code",
[
(False, 0, False, 0),
(True, 0, False, 0),
(True, 5, False, 0),
@ -12,83 +13,83 @@ import pytest
(False, 0, True, 5),
(True, 0, True, 0),
(True, 5, True, 5),
], ids=[
'no-hooks',
'pre-success',
'pre-fail',
'post-success',
'post-fail',
'pre-post-success',
'pre-post-fail',
])
@pytest.mark.parametrize('cmd', ['--version', 'version'])
def test_hooks(
runner, yadm_cmd, paths, cmd,
pre, pre_code, post, post_code):
],
ids=[
"no-hooks",
"pre-success",
"pre-fail",
"post-success",
"post-fail",
"pre-post-success",
"pre-post-fail",
],
)
@pytest.mark.parametrize("cmd", ["--version", "version"])
def test_hooks(runner, yadm_cmd, paths, cmd, pre, pre_code, post, post_code):
"""Test pre/post hook"""
# generate hooks
if pre:
create_hook(paths, 'pre_version', pre_code)
create_hook(paths, "pre_version", pre_code)
if post:
create_hook(paths, 'post_version', post_code)
create_hook(paths, "post_version", post_code)
# run yadm
run = runner(yadm_cmd(cmd))
# when a pre hook fails, yadm should exit with the hook's code
assert run.code == pre_code
assert run.err == ''
assert run.err == ""
if pre:
assert 'HOOK:pre_version' in run.out
assert "HOOK:pre_version" in run.out
# if pre hook is missing or successful, yadm itself should exit 0
if run.success:
if post:
assert 'HOOK:post_version' in run.out
assert "HOOK:post_version" in run.out
else:
# when a pre hook fails, yadm should not run the command
assert 'version will not be run' in run.out
assert "version will not be run" in run.out
# when a pre hook fails, yadm should not run the post hook
assert 'HOOK:post_version' not in run.out
assert "HOOK:post_version" not in run.out
# repo fixture is needed to test the population of YADM_HOOK_WORK
@pytest.mark.usefixtures('ds1_repo_copy')
@pytest.mark.usefixtures("ds1_repo_copy")
def test_hook_env(runner, yadm_cmd, paths):
"""Test hook environment"""
# test will be done with a non existent "git" passthru command
# which should exit with a failing code
cmd = 'passthrucmd'
cmd = "passthrucmd"
# write the hook
hook = paths.hooks.join(f'post_{cmd}')
hook.write('#!/bin/bash\nenv\ndeclare\n')
hook = paths.hooks.join(f"post_{cmd}")
hook.write("#!/bin/bash\nenv\ndeclare\n")
hook.chmod(0o755)
run = runner(yadm_cmd(cmd, 'extra_args'))
run = runner(yadm_cmd(cmd, "extra_args"))
# expect passthru to fail
assert run.failure
assert f"'{cmd}' is not a git command" in run.err
# verify hook environment
assert 'YADM_HOOK_EXIT=1\n' in run.out
assert f'YADM_HOOK_COMMAND={cmd}\n' in run.out
assert f'YADM_HOOK_DIR={paths.yadm}\n' in run.out
assert f'YADM_HOOK_FULL_COMMAND={cmd} extra_args\n' in run.out
assert f'YADM_HOOK_REPO={paths.repo}\n' in run.out
assert f'YADM_HOOK_WORK={paths.work}\n' in run.out
assert 'YADM_ENCRYPT_INCLUDE_FILES=\n' in run.out
assert "YADM_HOOK_EXIT=1\n" in run.out
assert f"YADM_HOOK_COMMAND={cmd}\n" in run.out
assert f"YADM_HOOK_DIR={paths.yadm}\n" in run.out
assert f"YADM_HOOK_FULL_COMMAND={cmd} extra_args\n" in run.out
assert f"YADM_HOOK_REPO={paths.repo}\n" in run.out
assert f"YADM_HOOK_WORK={paths.work}\n" in run.out
assert "YADM_ENCRYPT_INCLUDE_FILES=\n" in run.out
# verify the hook environment contains certain exported functions
for func in [
'builtin_dirname',
'relative_path',
'unix_path',
'mixed_path',
"builtin_dirname",
"relative_path",
"unix_path",
"mixed_path",
]:
assert f'BASH_FUNC_{func}' in run.out
assert f"BASH_FUNC_{func}" in run.out
# verify the hook environment contains the list of encrypted files
script = f"""
@ -98,10 +99,10 @@ def test_hook_env(runner, yadm_cmd, paths):
ENCRYPT_INCLUDE_FILES=(a b c)
invoke_hook "post"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'YADM_ENCRYPT_INCLUDE_FILES=a\nb\nc\n' in run.out
assert run.err == ""
assert "YADM_ENCRYPT_INCLUDE_FILES=a\nb\nc\n" in run.out
def test_escaped(runner, yadm_cmd, paths):
@ -109,35 +110,33 @@ def test_escaped(runner, yadm_cmd, paths):
# test will be done with a non existent "git" passthru command
# which should exit with a failing code
cmd = 'passthrucmd'
cmd = "passthrucmd"
# write the hook
hook = paths.hooks.join(f'post_{cmd}')
hook.write('#!/bin/bash\nenv\n')
hook = paths.hooks.join(f"post_{cmd}")
hook.write("#!/bin/bash\nenv\n")
hook.chmod(0o755)
run = runner(yadm_cmd(cmd, 'a b', 'c\td', 'e\\f'))
run = runner(yadm_cmd(cmd, "a b", "c\td", "e\\f"))
# expect passthru to fail
assert run.failure
# verify escaped values
assert (
f'YADM_HOOK_FULL_COMMAND={cmd} '
'a\\ b c\\\td e\\\\f\n') in run.out
assert f"YADM_HOOK_FULL_COMMAND={cmd} a\\ b c\\\td e\\\\f\n" in run.out
@pytest.mark.parametrize('condition', ['exec', 'no-exec', 'mingw'])
@pytest.mark.parametrize("condition", ["exec", "no-exec", "mingw"])
def test_executable(runner, paths, condition):
"""Verify hook must be exectuable"""
cmd = 'version'
hook = paths.hooks.join(f'pre_{cmd}')
hook.write('#!/bin/sh\necho HOOK\n')
cmd = "version"
hook = paths.hooks.join(f"pre_{cmd}")
hook.write("#!/bin/sh\necho HOOK\n")
hook.chmod(0o644)
if condition == 'exec':
if condition == "exec":
hook.chmod(0o755)
mingw = 'OPERATING_SYSTEM="MINGWx"' if condition == 'mingw' else ''
mingw = 'OPERATING_SYSTEM="MINGWx"' if condition == "mingw" else ""
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_HOOKS="{paths.hooks}"
@ -145,27 +144,23 @@ def test_executable(runner, paths, condition):
{mingw}
invoke_hook "pre"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
if condition != 'mingw':
if condition != "mingw":
assert run.success
assert run.err == ''
assert run.err == ""
else:
assert run.failure
assert 'Permission denied' in run.err
assert "Permission denied" in run.err
if condition == 'exec':
assert 'HOOK' in run.out
elif condition == 'no-exec':
assert 'HOOK' not in run.out
if condition == "exec":
assert "HOOK" in run.out
elif condition == "no-exec":
assert "HOOK" not in run.out
def create_hook(paths, name, code):
"""Create hook"""
hook = paths.hooks.join(name)
hook.write(
'#!/bin/sh\n'
f'echo HOOK:{name}\n'
f'exit {code}\n'
)
hook.write("#!/bin/sh\n" f"echo HOOK:{name}\n" f"exit {code}\n")
hook.chmod(0o755)

View file

@ -4,22 +4,24 @@ import pytest
@pytest.mark.parametrize(
'alt_work, repo_present, force', [
"alt_work, repo_present, force",
[
(False, False, False),
(True, False, False),
(False, True, False),
(False, True, True),
(True, True, True),
], ids=[
'simple',
'-w',
'existing repo',
'-f',
'-w & -f',
])
@pytest.mark.usefixtures('ds1_work_copy')
def test_init(
runner, yadm_cmd, paths, repo_config, alt_work, repo_present, force):
],
ids=[
"simple",
"-w",
"existing repo",
"-f",
"-w & -f",
],
)
@pytest.mark.usefixtures("ds1_work_copy")
def test_init(runner, yadm_cmd, paths, repo_config, alt_work, repo_present, force):
"""Test init
Repos should have attribs:
@ -31,54 +33,54 @@ def test_init(
"""
# these tests will assume this for $HOME
home = str(paths.root.mkdir('HOME'))
home = str(paths.root.mkdir("HOME"))
# ds1_work_copy comes WITH an empty repo dir present.
old_repo = paths.repo.join('old_repo')
old_repo = paths.repo.join("old_repo")
if repo_present:
# Let's put some data in it, so we can confirm that data is gone when
# forced to be overwritten.
old_repo.write('old repo data')
old_repo.write("old repo data")
assert old_repo.isfile()
else:
paths.repo.remove()
# command args
args = ['init']
args = ["init"]
cwd = None
if alt_work:
if force:
cwd = paths.work.dirname
args.extend(['-w', paths.work.basename])
args.extend(["-w", paths.work.basename])
else:
args.extend(['-w', paths.work])
args.extend(["-w", paths.work])
if force:
args.append('-f')
args.append("-f")
# run init
run = runner(yadm_cmd(*args), env={'HOME': home}, cwd=cwd)
runner(["git", "config", "--global", "init.defaultBranch", "master"], env={"HOME": home}, cwd=cwd)
run = runner(yadm_cmd(*args), env={"HOME": home}, cwd=cwd)
if repo_present and not force:
assert run.failure
assert 'repo already exists' in run.err
assert old_repo.isfile(), 'Missing original repo'
assert "repo already exists" in run.err
assert old_repo.isfile(), "Missing original repo"
else:
assert run.success
assert 'Initialized empty shared Git repository' in run.out
assert "Initialized empty shared Git repository" in run.out
if repo_present:
assert not old_repo.isfile(), 'Original repo still exists'
assert not old_repo.isfile(), "Original repo still exists"
else:
assert run.err == ''
assert run.err == ""
if alt_work:
assert repo_config('core.worktree') == paths.work
assert repo_config("core.worktree") == paths.work
else:
assert repo_config('core.worktree') == home
assert repo_config("core.worktree") == home
# uniform repo assertions
assert oct(paths.repo.stat().mode).endswith('00'), (
'Repo is not secure')
assert repo_config('core.bare') == 'false'
assert repo_config('status.showUntrackedFiles') == 'no'
assert repo_config('yadm.managed') == 'true'
assert oct(paths.repo.stat().mode).endswith("00"), "Repo is not secure"
assert repo_config("core.bare") == "false"
assert repo_config("status.showUntrackedFiles") == "no"
assert repo_config("yadm.managed") == "true"

View file

@ -4,38 +4,38 @@ import pytest
@pytest.mark.parametrize(
'name', [
'',
'invalid',
'commands',
'configs',
'repo',
'switches',
])
def test_introspect_category(
runner, yadm_cmd, paths, name,
supported_commands, supported_configs, supported_switches):
"name",
[
"",
"invalid",
"commands",
"configs",
"repo",
"switches",
],
)
def test_introspect_category(runner, yadm_cmd, paths, name, supported_commands, supported_configs, supported_switches):
"""Validate introspection category"""
if name:
run = runner(command=yadm_cmd('introspect', name))
run = runner(command=yadm_cmd("introspect", name))
else:
run = runner(command=yadm_cmd('introspect'))
run = runner(command=yadm_cmd("introspect"))
assert run.success
assert run.err == ''
assert run.err == ""
expected = []
if name == 'commands':
if name == "commands":
expected = supported_commands
elif name == 'configs':
elif name == "configs":
expected = supported_configs
elif name == 'switches':
elif name == "switches":
expected = supported_switches
# assert values
if name in ('', 'invalid'):
assert run.out == ''
if name == 'repo':
if name in ("", "invalid"):
assert run.out == ""
if name == "repo":
assert run.out.rstrip() == paths.repo
# make sure every expected value is present

View file

@ -1,31 +1,35 @@
"""Test list"""
import os
import pytest
@pytest.mark.parametrize(
'location', [
'work',
'outside',
'subdir',
])
@pytest.mark.usefixtures('ds1_copy')
"location",
[
"work",
"outside",
"subdir",
],
)
@pytest.mark.usefixtures("ds1_copy")
def test_list(runner, yadm_cmd, paths, ds1, location):
"""List tests"""
if location == 'work':
if location == "work":
run_dir = paths.work
elif location == 'outside':
run_dir = paths.work.join('..')
elif location == 'subdir':
elif location == "outside":
run_dir = paths.work.join("..")
else:
assert location == "subdir"
# first directory with tracked data
run_dir = paths.work.join(ds1.tracked_dirs[0])
with run_dir.as_cwd():
# test with '-a'
# should get all tracked files, relative to the work path
run = runner(command=yadm_cmd('list', '-a'))
run = runner(command=yadm_cmd("list", "-a"))
assert run.success
assert run.err == ''
assert run.err == ""
returned_files = set(run.out.splitlines())
expected_files = {e.path for e in ds1 if e.tracked}
assert returned_files == expected_files
@ -33,16 +37,14 @@ def test_list(runner, yadm_cmd, paths, ds1, location):
# should get all tracked files, relative to the work path unless in a
# subdir, then those should be a limited set of files, relative to the
# subdir
run = runner(command=yadm_cmd('list'))
run = runner(command=yadm_cmd("list"))
assert run.success
assert run.err == ''
assert run.err == ""
returned_files = set(run.out.splitlines())
if location == 'subdir':
if location == "subdir":
basepath = os.path.basename(os.getcwd())
# only expect files within the subdir
# names should be relative to subdir
expected_files = {
e.path[len(basepath)+1:]
for e in ds1 if e.tracked and e.path.startswith(basepath)
}
index = len(basepath) + 1
expected_files = {e.path[index:] for e in ds1 if e.tracked and e.path.startswith(basepath)}
assert returned_files == expected_files

View file

@ -1,21 +1,21 @@
"""Test perms"""
import os
import pytest
@pytest.mark.parametrize('autoperms', ['notest', 'unset', 'true', 'false'])
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize("autoperms", ["notest", "unset", "true", "false"])
@pytest.mark.usefixtures("ds1_copy")
def test_perms(runner, yadm_cmd, paths, ds1, autoperms):
"""Test perms"""
# set the value of auto-perms
if autoperms != 'notest':
if autoperms != 'unset':
os.system(' '.join(
yadm_cmd('config', 'yadm.auto-perms', autoperms)))
if autoperms != "notest":
if autoperms != "unset":
os.system(" ".join(yadm_cmd("config", "yadm.auto-perms", autoperms)))
# privatepaths will hold all paths that should become secured
privatepaths = [paths.work.join('.ssh'), paths.work.join('.gnupg')]
privatepaths = [paths.work.join(".ssh"), paths.work.join(".gnupg")]
privatepaths += [paths.work.join(private.path) for private in ds1.private]
# create an archive file
@ -23,82 +23,71 @@ def test_perms(runner, yadm_cmd, paths, ds1, autoperms):
privatepaths.append(paths.archive)
# create encrypted file test data
efile1 = paths.work.join('efile1')
efile1.write('efile1')
efile2 = paths.work.join('efile2')
efile2.write('efile2')
paths.encrypt.write('efile1\nefile2\n!efile1\n')
efile1 = paths.work.join("efile1")
efile1.write("efile1")
efile2 = paths.work.join("efile2")
efile2.write("efile2")
paths.encrypt.write("efile1\nefile2\n!efile1\n")
insecurepaths = [efile1]
privatepaths.append(efile2)
# assert these paths begin unsecured
for private in privatepaths + insecurepaths:
assert not oct(private.stat().mode).endswith('00'), (
'Path started secured')
assert not oct(private.stat().mode).endswith("00"), "Path started secured"
cmd = 'perms'
if autoperms != 'notest':
cmd = 'status'
run = runner(yadm_cmd(cmd), env={'HOME': paths.work})
cmd = "perms"
if autoperms != "notest":
cmd = "status"
run = runner(yadm_cmd(cmd), env={"HOME": paths.work})
assert run.success
assert run.err == ''
if cmd == 'perms':
assert run.out == ''
assert run.err == ""
if cmd == "perms":
assert run.out == ""
# these paths should be secured if processing perms
for private in privatepaths:
if autoperms == 'false':
assert not oct(private.stat().mode).endswith('00'), (
'Path should not be secured')
if autoperms == "false":
assert not oct(private.stat().mode).endswith("00"), "Path should not be secured"
else:
assert oct(private.stat().mode).endswith('00'), (
'Path has not been secured')
assert oct(private.stat().mode).endswith("00"), "Path has not been secured"
# these paths should never be secured
for private in insecurepaths:
assert not oct(private.stat().mode).endswith('00'), (
'Path should not be secured')
assert not oct(private.stat().mode).endswith("00"), "Path should not be secured"
@pytest.mark.parametrize('sshperms', [None, 'true', 'false'])
@pytest.mark.parametrize('gpgperms', [None, 'true', 'false'])
@pytest.mark.usefixtures('ds1_copy')
@pytest.mark.parametrize("sshperms", [None, "true", "false"])
@pytest.mark.parametrize("gpgperms", [None, "true", "false"])
@pytest.mark.usefixtures("ds1_copy")
def test_perms_control(runner, yadm_cmd, paths, ds1, sshperms, gpgperms):
"""Test fine control of perms"""
# set the value of ssh-perms
if sshperms:
os.system(' '.join(yadm_cmd('config', 'yadm.ssh-perms', sshperms)))
os.system(" ".join(yadm_cmd("config", "yadm.ssh-perms", sshperms)))
# set the value of gpg-perms
if gpgperms:
os.system(' '.join(yadm_cmd('config', 'yadm.gpg-perms', gpgperms)))
os.system(" ".join(yadm_cmd("config", "yadm.gpg-perms", gpgperms)))
# privatepaths will hold all paths that should become secured
privatepaths = [paths.work.join('.ssh'), paths.work.join('.gnupg')]
privatepaths = [paths.work.join(".ssh"), paths.work.join(".gnupg")]
privatepaths += [paths.work.join(private.path) for private in ds1.private]
# assert these paths begin unsecured
for private in privatepaths:
assert not oct(private.stat().mode).endswith('00'), (
'Path started secured')
assert not oct(private.stat().mode).endswith("00"), "Path started secured"
run = runner(yadm_cmd('perms'), env={'HOME': paths.work})
run = runner(yadm_cmd("perms"), env={"HOME": paths.work})
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
# these paths should be secured if processing perms
for private in privatepaths:
if (
(sshperms == 'false' and 'ssh' in str(private))
or
(gpgperms == 'false' and 'gnupg' in str(private))
):
assert not oct(private.stat().mode).endswith('00'), (
'Path should not be secured')
if (sshperms == "false" and "ssh" in str(private)) or (gpgperms == "false" and "gnupg" in str(private)):
assert not oct(private.stat().mode).endswith("00"), "Path should not be secured"
else:
assert oct(private.stat().mode).endswith('00'), (
'Path has not been secured')
assert oct(private.stat().mode).endswith("00"), "Path has not been secured"
# verify permissions aren't changed for the worktree
assert oct(paths.work.stat().mode).endswith('0755')
assert oct(paths.work.stat().mode).endswith("0755")

View file

@ -1,65 +1,88 @@
"""Syntax checks"""
import os
import shutil
import pytest
def test_yadm_syntax(runner, yadm):
"""Is syntactically valid"""
run = runner(command=['bash', '-n', yadm])
run = runner(command=["bash", "-n", yadm])
assert run.success
def test_shellcheck(pytestconfig, runner, yadm, shellcheck_version):
"""Passes shellcheck"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=['shellcheck', '-V'], report=False)
if f'version: {shellcheck_version}' not in run.out:
pytest.skip('Unsupported shellcheck version')
run = runner(command=['shellcheck', '-s', 'bash', yadm])
run = runner(command=["shellcheck", "-V"], report=False)
if f"version: {shellcheck_version}" not in run.out:
pytest.skip("Unsupported shellcheck version")
run = runner(command=["shellcheck", "-s", "bash", yadm])
assert run.success
def test_pylint(pytestconfig, runner, pylint_version):
"""Passes pylint"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=['pylint', '--version'], report=False)
if f'pylint {pylint_version}' not in run.out:
pytest.skip('Unsupported pylint version')
pyfiles = list()
for tfile in os.listdir('test'):
if tfile.endswith('.py'):
pyfiles.append(f'test/{tfile}')
run = runner(command=['pylint'] + pyfiles)
run = runner(command=["pylint", "--version"], report=False)
if f"pylint {pylint_version}" not in run.out:
pytest.skip("Unsupported pylint version")
pyfiles = []
for tfile in os.listdir("test"):
if tfile.endswith(".py"):
pyfiles.append(f"test/{tfile}")
run = runner(command=["pylint"] + pyfiles)
assert run.success
def test_isort(pytestconfig, runner, isort_version):
"""Passes isort"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=["isort", "--version"], report=False)
if isort_version not in run.out:
pytest.skip("Unsupported isort version")
run = runner(command=["isort", "-c", "test"])
assert run.success
def test_flake8(pytestconfig, runner, flake8_version):
"""Passes flake8"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=['flake8', '--version'], report=False)
run = runner(command=["flake8", "--version"], report=False)
if not run.out.startswith(flake8_version):
pytest.skip('Unsupported flake8 version')
run = runner(command=['flake8', 'test'])
pytest.skip("Unsupported flake8 version")
run = runner(command=["flake8", "test"])
assert run.success
def test_black(pytestconfig, runner, black_version):
"""Passes black"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=["black", "--version"], report=False)
if black_version not in run.out:
pytest.skip("Unsupported black version")
run = runner(command=["black", "--check", "test"])
assert run.success
def test_yamllint(pytestconfig, runner, yamllint_version):
"""Passes yamllint"""
if not pytestconfig.getoption("--force-linters"):
run = runner(command=['yamllint', '--version'], report=False)
run = runner(command=["yamllint", "--version"], report=False)
if not run.out.strip().endswith(yamllint_version):
pytest.skip('Unsupported yamllint version')
run = runner(
command=['yamllint', '-s', '$(find . -name \\*.yml)'],
shell=True)
pytest.skip("Unsupported yamllint version")
run = runner(command=["yamllint", "-s", "$(find . -name \\*.yml)"], shell=True)
assert run.success
def test_man(runner):
"""Check for warnings from man"""
run = runner(
command=['man', '--warnings', './yadm.1'])
if shutil.which("mandoc"):
command = ["mandoc", "-T", "lint"]
else:
command = ["groff", "-ww", "-z"]
run = runner(command=command + ["-man", "./yadm.1"])
assert run.success
assert run.err == ''
assert 'yadm - Yet Another Dotfiles Manager' in run.out
assert run.out == ""
assert run.err == ""

View file

@ -8,14 +8,14 @@ def test_bootstrap_missing(runner, paths):
def test_bootstrap_no_exec(runner, paths):
"""Test result of bootstrap_available, when bootstrap not executable"""
paths.bootstrap.write('')
paths.bootstrap.write("")
paths.bootstrap.chmod(0o644)
run_test(runner, paths, False)
def test_bootstrap_exec(runner, paths):
"""Test result of bootstrap_available, when bootstrap executable"""
paths.bootstrap.write('')
paths.bootstrap.write("")
paths.bootstrap.chmod(0o775)
run_test(runner, paths, True)
@ -27,7 +27,7 @@ def run_test(runner, paths, success):
YADM_BOOTSTRAP='{paths.bootstrap}'
bootstrap_available
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success == success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""

View file

@ -1,61 +0,0 @@
"""Unit tests: choose_template_cmd"""
import pytest
@pytest.mark.parametrize('label', ['', 'default', 'other'])
@pytest.mark.parametrize('awk', [True, False], ids=['awk', 'no-awk'])
def test_kind_default(runner, yadm, awk, label):
"""Test kind: default"""
expected = 'template_default'
awk_avail = 'true'
if not awk:
awk_avail = 'false'
expected = ''
if label == 'other':
expected = ''
script = f"""
YADM_TEST=1 source {yadm}
function awk_available {{ { awk_avail}; }}
template="$(choose_template_cmd "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert f'TEMPLATE:{expected}\n' in run.out
@pytest.mark.parametrize('label', ['envtpl', 'j2cli', 'j2', 'other'])
@pytest.mark.parametrize('envtpl', [True, False], ids=['envtpl', 'no-envtpl'])
@pytest.mark.parametrize('j2cli', [True, False], ids=['j2cli', 'no-j2cli'])
def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
"""Test kind: j2 (both j2cli & envtpl)
j2cli is preferred over envtpl if available.
"""
envtpl_avail = 'true' if envtpl else 'false'
j2cli_avail = 'true' if j2cli else 'false'
if label in ('j2cli', 'j2') and j2cli:
expected = 'template_j2cli'
elif label in ('envtpl', 'j2') and envtpl:
expected = 'template_envtpl'
else:
expected = ''
script = f"""
YADM_TEST=1 source {yadm}
function envtpl_available {{ { envtpl_avail}; }}
function j2cli_available {{ { j2cli_avail}; }}
template="$(choose_template_cmd "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert f'TEMPLATE:{expected}\n' in run.out

View file

@ -0,0 +1,62 @@
"""Unit tests: choose_template_processor"""
import pytest
@pytest.mark.parametrize("label", ["", "default", "other"])
@pytest.mark.parametrize("awk", [True, False], ids=["awk", "no-awk"])
def test_kind_default(runner, yadm, awk, label):
"""Test kind: default"""
expected = "default"
awk_avail = "true"
if not awk:
awk_avail = "false"
expected = ""
if label == "other":
expected = ""
script = f"""
YADM_TEST=1 source {yadm}
function awk_available {{ {awk_avail}; }}
template="$(choose_template_processor "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert f"TEMPLATE:{expected}\n" in run.out
@pytest.mark.parametrize("label", ["envtpl", "j2cli", "j2", "other"])
@pytest.mark.parametrize("envtpl", [True, False], ids=["envtpl", "no-envtpl"])
@pytest.mark.parametrize("j2cli", [True, False], ids=["j2cli", "no-j2cli"])
def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
"""Test kind: j2 (both j2cli & envtpl)
j2cli is preferred over envtpl if available.
"""
envtpl_avail = "true" if envtpl else "false"
j2cli_avail = "true" if j2cli else "false"
if label in ("j2cli", "j2") and j2cli:
expected = "j2cli"
elif label in ("envtpl", "j2") and envtpl:
expected = "envtpl"
else:
expected = ""
script = f"""
YADM_TEST=1 source {yadm}
function envtpl_available {{ {envtpl_avail}; }}
function j2cli_available {{ {j2cli_avail}; }}
template="$(choose_template_processor "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert f"TEMPLATE:{expected}\n" in run.out

View file

@ -2,52 +2,56 @@
import pytest
ARCHIVE = 'archive'
BOOTSTRAP = 'bootstrap'
CONFIG = 'config'
ENCRYPT = 'encrypt'
HOME = '/testhome'
REPO = 'repo.git'
YDIR = '.config/yadm'
YDATA = '.local/share/yadm'
ARCHIVE = "archive"
BOOTSTRAP = "bootstrap"
CONFIG = "config"
ENCRYPT = "encrypt"
HOME = "/testhome"
REPO = "repo.git"
YDIR = ".config/yadm"
YDATA = ".local/share/yadm"
@pytest.mark.parametrize(
'override, expect', [
"override, expect",
[
(None, {}),
('-Y', {'yadm': 'YADM_DIR'}),
('--yadm-data', {'data': 'YADM_DATA'}),
('--yadm-repo', {'repo': 'YADM_REPO', 'git': 'GIT_DIR'}),
('--yadm-config', {'config': 'YADM_CONFIG'}),
('--yadm-encrypt', {'encrypt': 'YADM_ENCRYPT'}),
('--yadm-archive', {'archive': 'YADM_ARCHIVE'}),
('--yadm-bootstrap', {'bootstrap': 'YADM_BOOTSTRAP'}),
], ids=[
'default',
'override yadm dir',
'override yadm data',
'override repo',
'override config',
'override encrypt',
'override archive',
'override bootstrap',
])
("-Y", {"yadm": "YADM_DIR"}),
("--yadm-data", {"data": "YADM_DATA"}),
("--yadm-repo", {"repo": "YADM_REPO", "git": "GIT_DIR"}),
("--yadm-config", {"config": "YADM_CONFIG"}),
("--yadm-encrypt", {"encrypt": "YADM_ENCRYPT"}),
("--yadm-archive", {"archive": "YADM_ARCHIVE"}),
("--yadm-bootstrap", {"bootstrap": "YADM_BOOTSTRAP"}),
],
ids=[
"default",
"override yadm dir",
"override yadm data",
"override repo",
"override config",
"override encrypt",
"override archive",
"override bootstrap",
],
)
@pytest.mark.parametrize(
'path', ['.', './override', 'override', '.override', '/override'], ids=[
'cwd', './relative', 'relative', 'hidden relative', 'absolute'
])
"path",
[".", "./override", "override", ".override", "/override"],
ids=["cwd", "./relative", "relative", "hidden relative", "absolute"],
)
def test_config(runner, paths, override, expect, path):
"""Test configure_paths"""
if path.startswith('/'):
if path.startswith("/"):
expected_path = path
else:
expected_path = str(paths.root.join(path))
args = [override, path] if override else []
if override == '-Y':
if override == "-Y":
matches = match_map(expected_path)
elif override == '--yadm-data':
elif override == "--yadm-data":
matches = match_map(None, expected_path)
else:
matches = match_map()
@ -61,23 +65,23 @@ def test_config(runner, paths, override, expect, path):
def match_map(yadm_dir=None, yadm_data=None):
"""Create a dictionary of matches, relative to yadm_dir"""
if not yadm_dir:
yadm_dir = '/'.join([HOME, YDIR])
yadm_dir = "/".join([HOME, YDIR])
if not yadm_data:
yadm_data = '/'.join([HOME, YDATA])
yadm_data = "/".join([HOME, YDATA])
return {
'yadm': f'YADM_DIR="{yadm_dir}"',
'repo': f'YADM_REPO="{yadm_data}/{REPO}"',
'config': f'YADM_CONFIG="{yadm_dir}/{CONFIG}"',
'encrypt': f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"',
'archive': f'YADM_ARCHIVE="{yadm_data}/{ARCHIVE}"',
'bootstrap': f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"',
'git': f'GIT_DIR="{yadm_data}/{REPO}"',
}
"yadm": f'YADM_DIR="{yadm_dir}"',
"repo": f'YADM_REPO="{yadm_data}/{REPO}"',
"config": f'YADM_CONFIG="{yadm_dir}/{CONFIG}"',
"encrypt": f'YADM_ENCRYPT="{yadm_dir}/{ENCRYPT}"',
"archive": f'YADM_ARCHIVE="{yadm_data}/{ARCHIVE}"',
"bootstrap": f'YADM_BOOTSTRAP="{yadm_dir}/{BOOTSTRAP}"',
"git": f'GIT_DIR="{yadm_data}/{REPO}"',
}
def run_test(runner, paths, args, expected_matches, cwd=None):
"""Run proces global args, and run configure_paths"""
argstring = ' '.join(['"'+a+'"' for a in args])
argstring = " ".join(['"' + a + '"' for a in args])
script = f"""
YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
process_global_args {argstring}
@ -85,10 +89,12 @@ def run_test(runner, paths, args, expected_matches, cwd=None):
XDG_DATA_HOME=
HOME="{HOME}" set_yadm_dirs
configure_paths
declare -p | grep -E '(YADM|GIT)_'
for var in "${{!YADM_@}}" "${{!GIT_@}}"; do
echo "$var=\\"${{!var}}\\""
done
"""
run = runner(command=['bash'], inp=script, cwd=cwd)
run = runner(command=["bash"], inp=script, cwd=cwd)
assert run.success
assert run.err == ''
assert run.err == ""
for match in expected_matches:
assert match in run.out

View file

@ -1,43 +1,43 @@
"""Unit tests: copy_perms"""
import os
import pytest
OCTAL = '7654'
NON_OCTAL = '9876'
OCTAL = "7654"
NON_OCTAL = "9876"
@pytest.mark.parametrize(
'stat_broken', [True, False], ids=['normal', 'stat broken'])
@pytest.mark.parametrize("stat_broken", [False, True], ids=["normal", "stat broken"])
def test_copy_perms(runner, yadm, tmpdir, stat_broken):
"""Test function copy_perms"""
src_mode = 0o754
dst_mode = 0o644
source = tmpdir.join('source')
source.write('test', ensure=True)
source = tmpdir.join("source")
source.write("test", ensure=True)
source.chmod(src_mode)
dest = tmpdir.join('dest')
dest.write('test', ensure=True)
dest = tmpdir.join("dest")
dest.write("test", ensure=True)
dest.chmod(dst_mode)
override_stat = ''
override_stat = ""
if stat_broken:
override_stat = 'function stat() { echo broken; }'
override_stat = "function stat() { echo broken; }"
script = f"""
YADM_TEST=1 source {yadm}
{override_stat}
copy_perms "{source}" "{dest}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.out == ''
assert run.err == ""
assert run.out == ""
expected = dst_mode if stat_broken else src_mode
assert oct(os.stat(dest).st_mode)[-3:] == oct(expected)[-3:]
@pytest.mark.parametrize(
'stat_output', [OCTAL, NON_OCTAL], ids=['octal', 'non-octal'])
@pytest.mark.parametrize("stat_output", [OCTAL, NON_OCTAL], ids=["octal", "non-octal"])
def test_get_mode(runner, yadm, stat_output):
"""Test function get_mode"""
script = f"""
@ -46,8 +46,8 @@ def test_get_mode(runner, yadm, stat_output):
mode=$(get_mode abc)
echo "MODE:$mode"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
expected = OCTAL if stat_output == OCTAL else ""
assert f'MODE:{expected}\n' in run.out
assert f"MODE:{expected}\n" in run.out

View file

@ -3,12 +3,12 @@
import pytest
@pytest.mark.parametrize('condition', ['default', 'override'])
@pytest.mark.parametrize("condition", ["default", "override"])
def test_get_cipher(runner, paths, condition):
"""Test _get_cipher()"""
if condition == 'override':
paths.config.write('[yadm]\n\tcipher = override-cipher')
if condition == "override":
paths.config.write("[yadm]\n\tcipher = override-cipher")
script = f"""
YADM_TEST=1 source {paths.pgm}
@ -19,18 +19,18 @@ def test_get_cipher(runner, paths, condition):
echo "output_archive:$output_archive"
echo "yadm_cipher:$yadm_cipher"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'output_archive:test-archive' in run.out
if condition == 'override':
assert 'yadm_cipher:override-cipher' in run.out
assert run.err == ""
assert "output_archive:test-archive" in run.out
if condition == "override":
assert "yadm_cipher:override-cipher" in run.out
else:
assert 'yadm_cipher:gpg' in run.out
assert "yadm_cipher:gpg" in run.out
@pytest.mark.parametrize('cipher', ['gpg', 'openssl', 'bad'])
@pytest.mark.parametrize('mode', ['_encrypt_to', '_decrypt_from'])
@pytest.mark.parametrize("cipher", ["gpg", "openssl", "bad"])
@pytest.mark.parametrize("mode", ["_encrypt_to", "_decrypt_from"])
def test_encrypt_decrypt(runner, paths, cipher, mode):
"""Test _encrypt_to() & _decrypt_from"""
@ -49,24 +49,24 @@ def test_encrypt_decrypt(runner, paths, cipher, mode):
GPG_PROGRAM=mock_gpg
{mode} {paths.archive}
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
if cipher != 'bad':
if cipher != "bad":
assert run.success
assert run.out.startswith(cipher)
assert str(paths.archive) in run.out
assert run.err == ''
assert run.err == ""
else:
assert run.failure
assert 'Unknown cipher' in run.err
assert "Unknown cipher" in run.err
@pytest.mark.parametrize('condition', ['default', 'override'])
@pytest.mark.parametrize("condition", ["default", "override"])
def test_get_openssl_ciphername(runner, paths, condition):
"""Test _get_openssl_ciphername()"""
if condition == 'override':
paths.config.write('[yadm]\n\topenssl-ciphername = override-cipher')
if condition == "override":
paths.config.write("[yadm]\n\topenssl-ciphername = override-cipher")
script = f"""
YADM_TEST=1 source {paths.pgm}
@ -76,21 +76,21 @@ def test_get_openssl_ciphername(runner, paths, condition):
result=$(_get_openssl_ciphername)
echo "result:$result"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if condition == 'override':
assert run.out.strip() == 'result:override-cipher'
assert run.err == ""
if condition == "override":
assert run.out.strip() == "result:override-cipher"
else:
assert run.out.strip() == 'result:aes-256-cbc'
assert run.out.strip() == "result:aes-256-cbc"
@pytest.mark.parametrize('condition', ['old', 'not-old'])
@pytest.mark.parametrize("condition", ["old", "not-old"])
def test_set_openssl_options(runner, paths, condition):
"""Test _set_openssl_options()"""
if condition == 'old':
paths.config.write('[yadm]\n\topenssl-old = true')
if condition == "old":
paths.config.write("[yadm]\n\topenssl-old = true")
script = f"""
YADM_TEST=1 source {paths.pgm}
@ -101,20 +101,20 @@ def test_set_openssl_options(runner, paths, condition):
_set_openssl_options
echo "result:${{OPENSSL_OPTS[@]}}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if condition == 'old':
assert '-testcipher -salt -md md5' in run.out
assert run.err == ""
if condition == "old":
assert "-testcipher -salt -md md5" in run.out
else:
assert '-testcipher -salt -pbkdf2 -iter 100000 -md sha512' in run.out
assert "-testcipher -salt -pbkdf2 -iter 100000 -md sha512" in run.out
@pytest.mark.parametrize('recipient', ['ASK', 'present', ''])
@pytest.mark.parametrize("recipient", ["ASK", "present", ""])
def test_set_gpg_options(runner, paths, recipient):
"""Test _set_gpg_options()"""
paths.config.write(f'[yadm]\n\tgpg-recipient = {recipient}')
paths.config.write(f"[yadm]\n\tgpg-recipient = {recipient}")
script = f"""
YADM_TEST=1 source {paths.pgm}
@ -124,12 +124,12 @@ def test_set_gpg_options(runner, paths, recipient):
_set_gpg_options
echo "result:${{GPG_OPTS[@]}}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if recipient == 'ASK':
assert run.out.strip() == 'result:--no-default-recipient -e'
elif recipient != '':
assert run.out.strip() == f'result:-e -r {recipient}'
assert run.err == ""
if recipient == "ASK":
assert run.out.strip() == "result:--no-default-recipient -e"
elif recipient != "":
assert run.out.strip() == f"result:-e -r {recipient}"
else:
assert run.out.strip() == 'result:-c'
assert run.out.strip() == "result:-c"

View file

@ -1,39 +1,35 @@
"""Unit tests: exclude_encrypted"""
import pytest
@pytest.mark.parametrize(
'exclude', ['missing', 'outdated', 'up-to-date'])
@pytest.mark.parametrize(
'encrypt_exists', [True, False], ids=['encrypt', 'no-encrypt'])
@pytest.mark.parametrize(
'auto_exclude', [True, False], ids=['enabled', 'disabled'])
def test_exclude_encrypted(
runner, tmpdir, yadm, encrypt_exists, auto_exclude, exclude):
@pytest.mark.parametrize("exclude", ["missing", "outdated", "up-to-date"])
@pytest.mark.parametrize("encrypt_exists", [True, False], ids=["encrypt", "no-encrypt"])
@pytest.mark.parametrize("auto_exclude", [True, False], ids=["enabled", "disabled"])
def test_exclude_encrypted(runner, tmpdir, yadm, encrypt_exists, auto_exclude, exclude):
"""Test exclude_encrypted()"""
header = (
"# yadm-auto-excludes\n"
"# This section is managed by yadm.\n"
"# Any edits below will be lost.\n"
)
header = """\
# yadm-auto-excludes
# This section is managed by yadm.
# Any edits below will be lost.
# yadm encrypt
"""
config_function = 'function config() { echo "false";}'
if auto_exclude:
config_function = 'function config() { return; }'
config_function = "function config() { return; }"
encrypt_file = tmpdir.join('encrypt_file')
repo_dir = tmpdir.join('repodir')
exclude_file = repo_dir.join('info/exclude')
encrypt_file = tmpdir.join("encrypt_file")
repo_dir = tmpdir.join("repodir")
exclude_file = repo_dir.join("info/exclude")
if encrypt_exists:
encrypt_file.write('test-encrypt-data\n', ensure=True)
if exclude == 'outdated':
exclude_file.write(
f'original-exclude\n{header}outdated\n', ensure=True)
elif exclude == 'up-to-date':
exclude_file.write(
f'original-exclude\n{header}test-encrypt-data\n', ensure=True)
encrypt_file.write("test-encrypt-data\n", ensure=True)
if exclude == "outdated":
exclude_file.write(f"original-exclude\n{header}outdated\n", ensure=True)
elif exclude == "up-to-date":
exclude_file.write(f"original-exclude\n{header}/test-encrypt-data\n", ensure=True)
script = f"""
YADM_TEST=1 source {yadm}
@ -43,24 +39,22 @@ def test_exclude_encrypted(
YADM_REPO="{repo_dir}"
exclude_encrypted
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
if auto_exclude:
if encrypt_exists:
assert exclude_file.exists()
if exclude == 'missing':
assert exclude_file.read() == f'{header}test-encrypt-data\n'
if exclude == "missing":
assert exclude_file.read() == f"{header}/test-encrypt-data\n"
else:
assert exclude_file.read() == (
'original-exclude\n'
f'{header}test-encrypt-data\n')
if exclude != 'up-to-date':
assert f'Updating {exclude_file}' in run.out
assert exclude_file.read() == ("original-exclude\n" f"{header}/test-encrypt-data\n")
if exclude != "up-to-date":
assert f"Updating {exclude_file}" in run.out
else:
assert run.out == ''
assert run.out == ""
else:
assert run.out == ''
assert run.out == ""
else:
assert run.out == ''
assert run.out == ""

View file

@ -1,27 +1,27 @@
"""Unit tests: issue_legacy_path_warning"""
import pytest
@pytest.mark.parametrize(
'legacy_path', [
"legacy_path",
[
None,
'repo.git',
'files.gpg',
],
)
@pytest.mark.parametrize(
'override', [True, False], ids=['override', 'no-override'])
@pytest.mark.parametrize(
'upgrade', [True, False], ids=['upgrade', 'no-upgrade'])
"repo.git",
"files.gpg",
],
)
@pytest.mark.parametrize("override", [True, False], ids=["override", "no-override"])
@pytest.mark.parametrize("upgrade", [True, False], ids=["upgrade", "no-upgrade"])
def test_legacy_warning(tmpdir, runner, yadm, upgrade, override, legacy_path):
"""Use issue_legacy_path_warning"""
home = tmpdir.mkdir('home')
home = tmpdir.mkdir("home")
if legacy_path:
home.ensure(f'.config/yadm/{str(legacy_path)}')
home.ensure(f".config/yadm/{str(legacy_path)}")
override = 'YADM_OVERRIDE_REPO=override' if override else ''
main_args = 'MAIN_ARGS=("upgrade")' if upgrade else ''
override = "YADM_OVERRIDE_REPO=override" if override else ""
main_args = 'MAIN_ARGS=("upgrade")' if upgrade else ""
script = f"""
XDG_CONFIG_HOME=
XDG_DATA_HOME=
@ -32,10 +32,10 @@ def test_legacy_warning(tmpdir, runner, yadm, upgrade, override, legacy_path):
set_yadm_dirs
issue_legacy_path_warning
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.out == ''
assert run.out == ""
if legacy_path and (not upgrade) and (not override):
assert 'Legacy paths have been detected' in run.err
assert "Legacy paths have been detected" in run.err
else:
assert 'Legacy paths have been detected' not in run.err
assert "Legacy paths have been detected" not in run.err

View file

@ -7,136 +7,143 @@ def test_not_called(runner, paths):
"""Test parse_encrypt (not called)"""
run = run_parse_encrypt(runner, paths, skip_parse=True)
assert run.success
assert run.err == ''
assert 'EIF:unparsed' in run.out, 'EIF should be unparsed'
assert 'EIF_COUNT:1' in run.out, 'Only value of EIF should be unparsed'
assert run.err == ""
assert "EIF:unparsed" in run.out, "EIF should be unparsed"
assert "EIF_COUNT:1" in run.out, "Only value of EIF should be unparsed"
def test_short_circuit(runner, paths):
"""Test parse_encrypt (short-circuit)"""
run = run_parse_encrypt(runner, paths, twice=True)
assert run.success
assert run.err == ''
assert 'PARSE_ENCRYPT_SHORT=parse_encrypt() not reprocessed' in run.out, (
'parse_encrypt() should short-circuit')
assert run.err == ""
assert "PARSE_ENCRYPT_SHORT=parse_encrypt() not reprocessed" in run.out, "parse_encrypt() should short-circuit"
@pytest.mark.parametrize(
'encrypt', [
('missing'),
('empty'),
])
"encrypt",
[
("missing"),
("empty"),
],
)
def test_empty(runner, paths, encrypt):
"""Test parse_encrypt (file missing/empty)"""
# write encrypt file
if encrypt == 'missing':
assert not paths.encrypt.exists(), 'Encrypt should be missing'
if encrypt == "missing":
assert not paths.encrypt.exists(), "Encrypt should be missing"
else:
paths.encrypt.write('')
assert paths.encrypt.exists(), 'Encrypt should exist'
assert paths.encrypt.size() == 0, 'Encrypt should be empty'
paths.encrypt.write("")
assert paths.encrypt.exists(), "Encrypt should exist"
assert paths.encrypt.size() == 0, "Encrypt should be empty"
# run parse_encrypt
run = run_parse_encrypt(runner, paths)
assert run.success
assert run.err == ''
assert run.err == ""
# validate parsing result
assert 'EIF_COUNT:0' in run.out, 'EIF should be empty'
assert "EIF_COUNT:0" in run.out, "EIF should be empty"
def create_test_encrypt_data(paths):
"""Generate test data for testing encrypt"""
edata = ''
edata = ""
expected = set()
# empty line
edata += '\n'
edata += "\n"
# simple comments
edata += '# a simple comment\n'
edata += ' # a comment with leading space\n'
edata += "# a simple comment\n"
edata += " # a comment with leading space\n"
# unreferenced directory
paths.work.join('unreferenced').mkdir()
paths.work.join("unreferenced").mkdir()
# simple files
edata += 'simple_file\n'
edata += 'simple.file\n'
paths.work.join('simple_file').write('')
paths.work.join('simple.file').write('')
paths.work.join('simple_file2').write('')
paths.work.join('simple.file2').write('')
expected.add('simple_file')
expected.add('simple.file')
edata += "simple_file\n"
edata += "simple.file\n"
paths.work.join("simple_file").write("")
paths.work.join("simple.file").write("")
paths.work.join("simple_file2").write("")
paths.work.join("simple.file2").write("")
expected.add("simple_file")
expected.add("simple.file")
# simple files in directories
edata += 'simple_dir/simple_file\n'
paths.work.join('simple_dir/simple_file').write('', ensure=True)
paths.work.join('simple_dir/simple_file2').write('', ensure=True)
expected.add('simple_dir/simple_file')
edata += "simple_dir/simple_file\n"
paths.work.join("simple_dir/simple_file").write("", ensure=True)
paths.work.join("simple_dir/simple_file2").write("", ensure=True)
expected.add("simple_dir/simple_file")
# paths with spaces
edata += 'with space/with space\n'
paths.work.join('with space/with space').write('', ensure=True)
paths.work.join('with space/with space2').write('', ensure=True)
expected.add('with space/with space')
edata += "with space/with space\n"
paths.work.join("with space/with space").write("", ensure=True)
paths.work.join("with space/with space2").write("", ensure=True)
expected.add("with space/with space")
# hidden files
edata += '.hidden\n'
paths.work.join('.hidden').write('')
expected.add('.hidden')
edata += ".hidden\n"
paths.work.join(".hidden").write("")
expected.add(".hidden")
# hidden files in directories
edata += '.hidden_dir/.hidden_file\n'
paths.work.join('.hidden_dir/.hidden_file').write('', ensure=True)
expected.add('.hidden_dir/.hidden_file')
edata += ".hidden_dir/.hidden_file\n"
paths.work.join(".hidden_dir/.hidden_file").write("", ensure=True)
expected.add(".hidden_dir/.hidden_file")
# wildcards
edata += 'wild*\n'
paths.work.join('wildcard1').write('', ensure=True)
paths.work.join('wildcard2').write('', ensure=True)
expected.add('wildcard1')
expected.add('wildcard2')
edata += "wild*\n"
edata += "*card1\n" # matches same file as the one above
paths.work.join("wildcard1").write("", ensure=True)
paths.work.join("wildcard2").write("", ensure=True)
paths.work.join("subdir/wildcard1").write("", ensure=True)
expected.add("wildcard1")
expected.add("wildcard2")
edata += 'dirwild*\n'
paths.work.join('dirwildcard/file1').write('', ensure=True)
paths.work.join('dirwildcard/file2').write('', ensure=True)
expected.add('dirwildcard')
edata += "dirwild*/file*\n"
paths.work.join("dirwildcard/file1").write("", ensure=True)
paths.work.join("dirwildcard/file2").write("", ensure=True)
expected.add("dirwildcard/file1")
expected.add("dirwildcard/file2")
# excludes
edata += 'exclude*\n'
edata += 'ex ex/*\n'
paths.work.join('exclude_file1').write('')
paths.work.join('exclude_file2.ex').write('')
paths.work.join('exclude_file3.ex3').write('')
expected.add('exclude_file1')
expected.add('exclude_file3.ex3')
edata += '!*.ex\n'
edata += '!ex ex/*.txt\n'
paths.work.join('ex ex/file4').write('', ensure=True)
paths.work.join('ex ex/file5.txt').write('', ensure=True)
paths.work.join('ex ex/file6.text').write('', ensure=True)
expected.add('ex ex/file4')
expected.add('ex ex/file6.text')
edata += "exclude*\n"
edata += "ex ex/*\n"
paths.work.join("exclude_file1").write("")
paths.work.join("exclude_file2.ex").write("")
paths.work.join("exclude_file3.ex3").write("")
expected.add("exclude_file1")
expected.add("exclude_file3.ex3")
edata += "!*.ex\n"
edata += "!ex ex/*.txt\n"
paths.work.join("ex ex/file4").write("", ensure=True)
paths.work.join("ex ex/file5.txt").write("", ensure=True)
paths.work.join("ex ex/file6.text").write("", ensure=True)
expected.add("ex ex/file4")
expected.add("ex ex/file6.text")
paths.work.join("dirwildcard/file7.ex").write("", ensure=True)
expected.add("dirwildcard/file7.ex")
# double star
edata += 'doublestar/**/file*\n'
edata += '!**/file3\n'
paths.work.join('doublestar/a/b/file1').write('', ensure=True)
paths.work.join('doublestar/c/d/file2').write('', ensure=True)
paths.work.join('doublestar/e/f/file3').write('', ensure=True)
paths.work.join('doublestar/g/h/nomatch').write('', ensure=True)
expected.add('doublestar/a/b/file1')
expected.add('doublestar/c/d/file2')
edata += "doublestar/**/file*\n"
edata += "!**/file3\n"
paths.work.join("doublestar/a/b/file1").write("", ensure=True)
paths.work.join("doublestar/c/d/file2").write("", ensure=True)
paths.work.join("doublestar/e/f/file3").write("", ensure=True)
paths.work.join("doublestar/g/h/nomatch").write("", ensure=True)
expected.add("doublestar/a/b/file1")
expected.add("doublestar/c/d/file2")
# doublestar/e/f/file3 is excluded
return edata, expected
@pytest.mark.usefixtures('ds1_repo_copy')
@pytest.mark.usefixtures("ds1_repo_copy")
def test_file_parse_encrypt(runner, paths):
"""Test parse_encrypt
@ -147,39 +154,35 @@ def test_file_parse_encrypt(runner, paths):
edata, expected = create_test_encrypt_data(paths)
# write encrypt file
print(f'ENCRYPT:\n---\n{edata}---\n')
print(f"ENCRYPT:\n---\n{edata}---\n")
paths.encrypt.write(edata)
assert paths.encrypt.isfile()
# run parse_encrypt
run = run_parse_encrypt(runner, paths)
assert run.success
assert run.err == ''
assert run.err == ""
assert f'EIF_COUNT:{len(expected)}' in run.out, 'EIF count wrong'
assert f"EIF_COUNT:{len(expected)}" in run.out, "EIF count wrong"
for expected_file in expected:
assert f'EIF:{expected_file}\n' in run.out
assert f"EIF:{expected_file}\n" in run.out
sorted_expectations = '\n'.join(
[f'EIF:{exp}' for exp in sorted(expected)])
sorted_expectations = "\n".join([f"EIF:{exp}" for exp in sorted(expected)])
assert sorted_expectations in run.out
def run_parse_encrypt(
runner, paths,
skip_parse=False,
twice=False):
def run_parse_encrypt(runner, paths, skip_parse=False, twice=False):
"""Run parse_encrypt
A count of ENCRYPT_INCLUDE_FILES will be reported as EIF_COUNT:X. All
values of ENCRYPT_INCLUDE_FILES will be reported as individual EIF:value
lines.
"""
parse_cmd = 'parse_encrypt'
parse_cmd = "parse_encrypt"
if skip_parse:
parse_cmd = ''
parse_cmd = ""
if twice:
parse_cmd = 'parse_encrypt; parse_encrypt'
parse_cmd = "parse_encrypt; parse_encrypt"
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_ENCRYPT={paths.encrypt}
@ -189,13 +192,11 @@ def run_parse_encrypt(
YADM_WORK={paths.work}
export YADM_WORK
{parse_cmd}
export ENCRYPT_INCLUDE_FILES
export PARSE_ENCRYPT_SHORT
env
echo PARSE_ENCRYPT_SHORT=$PARSE_ENCRYPT_SHORT
echo EIF_COUNT:${{#ENCRYPT_INCLUDE_FILES[@]}}
for value in "${{ENCRYPT_INCLUDE_FILES[@]}}"; do
echo "EIF:$value"
done
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
return run

View file

@ -1,17 +1,18 @@
"""Unit tests: private_dirs"""
import pytest
@pytest.mark.parametrize(
'gnupghome',
"gnupghome",
[True, False],
ids=['gnupghome-set', 'gnupghome-unset'],
ids=["gnupghome-set", "gnupghome-unset"],
)
@pytest.mark.parametrize('param', ['all', 'gnupg'])
@pytest.mark.parametrize("param", ["all", "gnupg"])
def test_relative_path(runner, paths, gnupghome, param):
"""Test translate_to_relative"""
alt_gnupghome = 'alt/gnupghome'
alt_gnupghome = "alt/gnupghome"
env_gnupghome = paths.work.join(alt_gnupghome)
script = f"""
@ -22,13 +23,13 @@ def test_relative_path(runner, paths, gnupghome, param):
env = {}
if gnupghome:
env['GNUPGHOME'] = env_gnupghome
env["GNUPGHOME"] = env_gnupghome
expected = alt_gnupghome if gnupghome else '.gnupg'
if param == 'all':
expected = f'.ssh {expected}'
expected = alt_gnupghome if gnupghome else ".gnupg"
if param == "all":
expected = f".ssh {expected}"
run = runner(command=['bash'], inp=script, env=env)
run = runner(command=["bash"], inp=script, env=env)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out.strip() == expected

View file

@ -1,19 +1,18 @@
"""Unit tests: query_distro"""
import pytest
@pytest.mark.parametrize(
'condition', ['lsb_release', 'os-release', 'os-release-quotes', 'missing'])
@pytest.mark.parametrize("condition", ["lsb_release", "os-release", "os-release-quotes", "missing"])
def test_query_distro(runner, yadm, tst_distro, tmp_path, condition):
"""Match lsb_release -si when present"""
test_release = 'testrelease'
lsb_release = ''
os_release = tmp_path.joinpath('os-release')
if 'os-release' in condition:
quotes = '"' if 'quotes' in condition else ''
os_release.write_text(
f"testing\nID={quotes}{test_release}{quotes}\nrelease")
if condition != 'lsb_release':
test_release = "testrelease"
lsb_release = ""
os_release = tmp_path.joinpath("os-release")
if "os-release" in condition:
quotes = '"' if "quotes" in condition else ""
os_release.write_text(f"testing\nID={quotes}{test_release}{quotes}\nrelease")
if condition != "lsb_release":
lsb_release = 'LSB_RELEASE_PROGRAM="missing_lsb_release"'
script = f"""
YADM_TEST=1 source {yadm}
@ -21,12 +20,12 @@ def test_query_distro(runner, yadm, tst_distro, tmp_path, condition):
OS_RELEASE="{os_release}"
query_distro
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if condition == 'lsb_release':
assert run.err == ""
if condition == "lsb_release":
assert run.out.rstrip() == tst_distro
elif 'os-release' in condition:
elif "os-release" in condition:
assert run.out.rstrip() == test_release
else:
assert run.out.rstrip() == ''
assert run.out.rstrip() == ""

View file

@ -1,26 +1,27 @@
"""Unit tests: query_distro_family"""
import pytest
@pytest.mark.parametrize(
'condition', ['os-release', 'os-release-quotes', 'missing'])
@pytest.mark.parametrize("condition", ["os-release", "os-release-quotes", "missing", "fallback"])
def test_query_distro_family(runner, yadm, tmp_path, condition):
"""Match ID_LIKE when present"""
test_family = 'testfamily'
os_release = tmp_path.joinpath('os-release')
if 'os-release' in condition:
quotes = '"' if 'quotes' in condition else ''
os_release.write_text(
f"testing\nID_LIKE={quotes}{test_family}{quotes}\nfamily")
test_family = "testfamily"
os_release = tmp_path.joinpath("os-release")
if "os-release" in condition:
quotes = '"' if "quotes" in condition else ""
os_release.write_text(f"testing\nID=test\nID_LIKE={quotes}{test_family}{quotes}\nfamily")
elif condition == "fallback":
os_release.write_text(f'testing\nID="{test_family}"\nfamily')
script = f"""
YADM_TEST=1 source {yadm}
OS_RELEASE="{os_release}"
query_distro_family
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if 'os-release' in condition:
assert run.out.rstrip() == test_family
assert run.err == ""
if condition == "missing":
assert run.out.rstrip() == ""
else:
assert run.out.rstrip() == ''
assert run.out.rstrip() == test_family

View file

@ -1,4 +1,5 @@
"""Unit tests: record_score"""
import pytest
INIT_VARS = """
@ -10,7 +11,7 @@ INIT_VARS = """
alt_scores=()
alt_targets=()
alt_sources=()
alt_template_cmds=()
alt_template_processors=()
"""
REPORT_RESULTS = """
@ -18,6 +19,7 @@ REPORT_RESULTS = """
echo "SCORES:${alt_scores[@]}"
echo "TARGETS:${alt_targets[@]}"
echo "SOURCES:${alt_sources[@]}"
echo "TEMPLATE_PROCESSORS:${alt_template_processors[@]}"
"""
@ -30,13 +32,14 @@ def test_dont_record_zeros(runner, yadm):
record_score "0" "testtgt" "testsrc"
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:0\n' in run.out
assert 'SCORES:\n' in run.out
assert 'TARGETS:\n' in run.out
assert 'SOURCES:\n' in run.out
assert run.err == ""
assert "SIZE:0\n" in run.out
assert "SCORES:\n" in run.out
assert "TARGETS:\n" in run.out
assert "SOURCES:\n" in run.out
assert "TEMPLATE_PROCESSORS:\n" in run.out
def test_new_scores(runner, yadm):
@ -45,34 +48,35 @@ def test_new_scores(runner, yadm):
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
record_score "1" "tgt_one" "src_one"
record_score "2" "tgt_two" "src_two"
record_score "4" "tgt_three" "src_three"
record_score "1" "tgt_one" "src_one" ""
record_score "2" "tgt_two" "src_two" ""
record_score "4" "tgt_three" "src_three" ""
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:3\n' in run.out
assert 'SCORES:1 2 4\n' in run.out
assert 'TARGETS:tgt_one tgt_two tgt_three\n' in run.out
assert 'SOURCES:src_one src_two src_three\n' in run.out
assert run.err == ""
assert "SIZE:3\n" in run.out
assert "SCORES:1 2 4\n" in run.out
assert "TARGETS:tgt_one tgt_two tgt_three\n" in run.out
assert "SOURCES:src_one src_two src_three\n" in run.out
assert "TEMPLATE_PROCESSORS: \n" in run.out
@pytest.mark.parametrize('difference', ['lower', 'equal', 'higher'])
@pytest.mark.parametrize("difference", ["lower", "equal", "higher"])
def test_existing_scores(runner, yadm, difference):
"""Test existing scores"""
expected_score = '2'
expected_src = 'existing_src'
if difference == 'lower':
score = '1'
elif difference == 'equal':
score = '2'
expected_score = "2"
expected_src = "existing_src"
if difference == "lower":
score = "1"
elif difference == "equal":
score = "2"
else:
score = '4'
expected_score = '4'
expected_src = 'new_src'
score = "4"
expected_score = "4"
expected_src = "new_src"
script = f"""
YADM_TEST=1 source {yadm}
@ -80,16 +84,18 @@ def test_existing_scores(runner, yadm, difference):
alt_scores=(2)
alt_targets=("testtgt")
alt_sources=("existing_src")
record_score "{score}" "testtgt" "new_src"
alt_template_processors=("")
record_score "{score}" "testtgt" "new_src" ""
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:1\n' in run.out
assert f'SCORES:{expected_score}\n' in run.out
assert 'TARGETS:testtgt\n' in run.out
assert f'SOURCES:{expected_src}\n' in run.out
assert run.err == ""
assert "SIZE:1\n" in run.out
assert f"SCORES:{expected_score}\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert f"SOURCES:{expected_src}\n" in run.out
assert "TEMPLATE_PROCESSORS:\n" in run.out
def test_existing_template(runner, yadm):
@ -100,42 +106,84 @@ def test_existing_template(runner, yadm):
{INIT_VARS}
alt_scores=(1)
alt_targets=("testtgt")
alt_sources=()
alt_template_cmds=("existing_template")
record_score "2" "testtgt" "new_src"
alt_sources=("src")
alt_template_processors=("existing_template")
record_score "2" "testtgt" "new_src" ""
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:1\n' in run.out
assert 'SCORES:1\n' in run.out
assert 'TARGETS:testtgt\n' in run.out
assert 'SOURCES:\n' in run.out
assert run.err == ""
assert "SIZE:1\n" in run.out
assert "SCORES:1\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert "SOURCES:src\n" in run.out
assert "TEMPLATE_PROCESSORS:existing_template\n" in run.out
def test_config_first(runner, yadm):
"""Verify YADM_CONFIG is always processed first"""
config = 'yadm_config_file'
config = "yadm_config_file"
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
YADM_CONFIG={config}
record_score "1" "tgt_before" "src_before"
record_template "tgt_tmp" "cmd_tmp" "src_tmp"
record_score "2" "{config}" "src_config"
record_score "3" "tgt_after" "src_after"
record_score "1" "tgt_before" "src_before" ""
record_score "1" "tgt_tmp" "src_tmp" "processor_tmp"
record_score "2" "{config}" "src_config" ""
record_score "3" "tgt_after" "src_after" ""
{REPORT_RESULTS}
echo "CMD_VALUE:${{alt_template_cmds[@]}}"
echo "CMD_INDEX:${{!alt_template_cmds[@]}}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:3\n' in run.out
assert 'SCORES:2 1 3\n' in run.out
assert f'TARGETS:{config} tgt_before tgt_tmp tgt_after\n' in run.out
assert 'SOURCES:src_config src_before src_tmp src_after\n' in run.out
assert 'CMD_VALUE:cmd_tmp\n' in run.out
assert 'CMD_INDEX:2\n' in run.out
assert run.err == ""
assert "SIZE:4\n" in run.out
assert "SCORES:2 1 1 3\n" in run.out
assert f"TARGETS:{config} tgt_before tgt_tmp tgt_after\n" in run.out
assert "SOURCES:src_config src_before src_tmp src_after\n" in run.out
assert "TEMPLATE_PROCESSORS: processor_tmp \n" in run.out
def test_new_template(runner, yadm):
"""Test new template"""
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
record_score 0 "tgt_one" "src_one" "processor_one"
record_score 0 "tgt_two" "src_two" "processor_two"
record_score 0 "tgt_three" "src_three" "processor_three"
{REPORT_RESULTS}
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert "SIZE:3\n" in run.out
assert "SCORES:0 0 0\n" in run.out
assert "TARGETS:tgt_one tgt_two tgt_three\n" in run.out
assert "SOURCES:src_one src_two src_three\n" in run.out
assert "TEMPLATE_PROCESSORS:processor_one processor_two processor_three\n" in run.out
def test_overwrite_existing_template(runner, yadm):
"""Overwrite existing templates"""
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
alt_scores=(0)
alt_targets=("testtgt")
alt_template_processors=("existing_processor")
alt_sources=("existing_src")
record_score 0 "testtgt" "new_src" "new_processor"
{REPORT_RESULTS}
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert "SIZE:1\n" in run.out
assert "SCORES:0\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert "SOURCES:new_src\n" in run.out
assert "TEMPLATE_PROCESSORS:new_processor\n" in run.out

View file

@ -1,55 +0,0 @@
"""Unit tests: record_template"""
INIT_VARS = """
alt_targets=()
alt_template_cmds=()
alt_sources=()
"""
REPORT_RESULTS = """
echo "SIZE:${#alt_targets[@]}"
echo "TARGETS:${alt_targets[@]}"
echo "CMDS:${alt_template_cmds[@]}"
echo "SOURCES:${alt_sources[@]}"
"""
def test_new_template(runner, yadm):
"""Test new template"""
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
record_template "tgt_one" "cmd_one" "src_one"
record_template "tgt_two" "cmd_two" "src_two"
record_template "tgt_three" "cmd_three" "src_three"
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:3\n' in run.out
assert 'TARGETS:tgt_one tgt_two tgt_three\n' in run.out
assert 'CMDS:cmd_one cmd_two cmd_three\n' in run.out
assert 'SOURCES:src_one src_two src_three\n' in run.out
def test_existing_template(runner, yadm):
"""Overwrite existing templates"""
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
alt_targets=("testtgt")
alt_template_cmds=("existing_cmd")
alt_sources=("existing_src")
record_template "testtgt" "new_cmd" "new_src"
{REPORT_RESULTS}
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert 'SIZE:1\n' in run.out
assert 'TARGETS:testtgt\n' in run.out
assert 'CMDS:new_cmd\n' in run.out
assert 'SOURCES:new_src\n' in run.out

View file

@ -1,20 +1,26 @@
"""Unit tests: relative_path"""
import pytest
@pytest.mark.parametrize(
'base,full_path,expected',
"base,full_path,expected",
[
("/A/B/C", "/A", "../.."),
("/A/B/C", "/A/B", ".."),
("/A/B/C", "/A/B/C", ""),
("/A/B/C", "/A/B/C/D", "D"),
("/A/B/C", "/A/B/C/D/E", "D/E"),
("/A/B/C", "/A/B/CD", "../CD"),
("/A/B/C", "/A/BB/C", "../../BB/C"),
("/A/B/C", "/A/B/D", "../D"),
("/A/B/C", "/A/B/D/E", "../D/E"),
("/A/B/C", "/A/D", "../../D"),
("/A/B/C", "/A/D/E", "../../D/E"),
("/A/B/C", "/D/E/F", "../../../D/E/F"),
("/", "/A/B/C", "A/B/C"),
("/A/B/C", "/", "../../.."),
("/A/B B/C", "/A/C C/D", "../../C C/D"),
],
)
def test_relative_path(runner, paths, base, full_path, expected):
@ -25,7 +31,7 @@ def test_relative_path(runner, paths, base, full_path, expected):
relative_path "{base}" "{full_path}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out.strip() == expected

View file

@ -1,37 +1,39 @@
"""Unit tests: remove_stale_links"""
import os
import pytest
@pytest.mark.parametrize('linked', [True, False])
@pytest.mark.parametrize('kind', ['file', 'symlink'])
@pytest.mark.parametrize("linked", [True, False])
@pytest.mark.parametrize("kind", ["file", "symlink"])
def test_remove_stale_links(runner, yadm, tmpdir, kind, linked):
"""Test remove_stale_links()"""
source_file = tmpdir.join('source_file')
source_file.write('source file', ensure=True)
link = tmpdir.join('link')
source_file = tmpdir.join("source_file")
source_file.write("source file", ensure=True)
link = tmpdir.join("link")
if kind == 'file':
link.write('link file', ensure=True)
if kind == "file":
link.write("link file", ensure=True)
else:
os.system(f'ln -s {source_file} {link}')
os.system(f"ln -s {source_file} {link}")
alt_linked = ''
alt_linked = ""
if linked:
alt_linked = source_file
script = f"""
YADM_TEST=1 source {yadm}
possible_alts=({link})
possible_alt_targets=({link})
alt_linked=({alt_linked})
function rm() {{ echo rm "$@"; }}
remove_stale_links
"""
run = runner(command=['bash'], inp=script)
assert run.err == ''
if kind == 'symlink' and not linked:
assert f'rm -f {link}' in run.out
run = runner(command=["bash"], inp=script)
assert run.err == ""
if kind == "symlink" and not linked:
assert f"rm -f {link}" in run.out
else:
assert run.out == ''
assert run.out == ""

View file

@ -1,16 +1,17 @@
"""Unit tests: report_invalid_alts"""
import pytest
@pytest.mark.parametrize('valid', [True, False], ids=['valid', 'no_valid'])
@pytest.mark.parametrize('previous', [True, False], ids=['prev', 'no_prev'])
@pytest.mark.parametrize("valid", [True, False], ids=["valid", "no_valid"])
@pytest.mark.parametrize("previous", [True, False], ids=["prev", "no_prev"])
def test_report_invalid_alts(runner, yadm, valid, previous):
"""Use report_invalid_alts"""
lwi = ''
alts = 'INVALID_ALT=()'
lwi = ""
alts = "INVALID_ALT=()"
if previous:
lwi = 'LEGACY_WARNING_ISSUED=1'
lwi = "LEGACY_WARNING_ISSUED=1"
if not valid:
alts = 'INVALID_ALT=("file##invalid")'
@ -20,11 +21,11 @@ def test_report_invalid_alts(runner, yadm, valid, previous):
{alts}
report_invalid_alts
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.out == ''
assert run.out == ""
if not valid and not previous:
assert 'WARNING' in run.err
assert 'file##invalid' in run.err
assert "WARNING" in run.err
assert "file##invalid" in run.err
else:
assert run.err == ''
assert run.err == ""

View file

@ -1,41 +1,42 @@
"""Unit tests: score_file"""
import pytest
CONDITION = {
'default': {
'labels': ['default'],
'modifier': 0,
},
'arch': {
'labels': ['a', 'arch'],
'modifier': 1,
},
'system': {
'labels': ['o', 'os'],
'modifier': 2,
},
'distro': {
'labels': ['d', 'distro'],
'modifier': 4,
},
'distro_family': {
'labels': ['f', 'distro_family'],
'modifier': 8,
},
'class': {
'labels': ['c', 'class'],
'modifier': 16,
},
'hostname': {
'labels': ['h', 'hostname'],
'modifier': 32,
},
'user': {
'labels': ['u', 'user'],
'modifier': 64,
},
}
TEMPLATE_LABELS = ['t', 'template', 'yadm']
"default": {
"labels": ["default"],
"modifier": 0,
},
"arch": {
"labels": ["a", "arch"],
"modifier": 1,
},
"system": {
"labels": ["o", "os"],
"modifier": 2,
},
"distro": {
"labels": ["d", "distro"],
"modifier": 4,
},
"distro_family": {
"labels": ["f", "distro_family"],
"modifier": 8,
},
"class": {
"labels": ["c", "class"],
"modifier": 16,
},
"hostname": {
"labels": ["h", "hostname"],
"modifier": 32,
},
"user": {
"labels": ["u", "user"],
"modifier": 64,
},
}
TEMPLATE_LABELS = ["t", "template", "yadm"]
def calculate_score(filename):
@ -43,162 +44,136 @@ def calculate_score(filename):
# pylint: disable=too-many-branches
score = 0
_, conditions = filename.split('##', 1)
_, conditions = filename.split("##", 1)
for condition in conditions.split(','):
for condition in conditions.split(","):
label = condition
value = None
if '.' in condition:
label, value = condition.split('.', 1)
if label in CONDITION['default']['labels']:
if "." in condition:
label, value = condition.split(".", 1)
if label in CONDITION["default"]["labels"]:
score += 1000
elif label in CONDITION['arch']['labels']:
if value == 'testarch':
score += 1000 + CONDITION['arch']['modifier']
elif label in CONDITION["arch"]["labels"]:
if value.lower() == "testarch":
score += 1000 + CONDITION["arch"]["modifier"]
else:
score = 0
break
elif label in CONDITION['system']['labels']:
if value == 'testsystem':
score += 1000 + CONDITION['system']['modifier']
elif label in CONDITION["system"]["labels"]:
if value.lower() == "testsystem":
score += 1000 + CONDITION["system"]["modifier"]
else:
score = 0
break
elif label in CONDITION['distro']['labels']:
if value == 'testdistro':
score += 1000 + CONDITION['distro']['modifier']
elif label in CONDITION["distro"]["labels"]:
if value.lower() == "testdistro":
score += 1000 + CONDITION["distro"]["modifier"]
else:
score = 0
break
elif label in CONDITION['class']['labels']:
if value == 'testclass':
score += 1000 + CONDITION['class']['modifier']
elif label in CONDITION["class"]["labels"]:
if value.lower() == "testclass":
score += 1000 + CONDITION["class"]["modifier"]
else:
score = 0
break
elif label in CONDITION['hostname']['labels']:
if value == 'testhost':
score += 1000 + CONDITION['hostname']['modifier']
elif label in CONDITION["hostname"]["labels"]:
if value.lower() == "testhost":
score += 1000 + CONDITION["hostname"]["modifier"]
else:
score = 0
break
elif label in CONDITION['user']['labels']:
if value == 'testuser':
score += 1000 + CONDITION['user']['modifier']
elif label in CONDITION["user"]["labels"]:
if value.lower() == "testuser":
score += 1000 + CONDITION["user"]["modifier"]
else:
score = 0
break
elif label in TEMPLATE_LABELS:
elif label not in TEMPLATE_LABELS:
score = 0
break
return score
@pytest.mark.parametrize(
'default', ['default', None], ids=['default', 'no-default'])
@pytest.mark.parametrize(
'arch', ['arch', None], ids=['arch', 'no-arch'])
@pytest.mark.parametrize(
'system', ['system', None], ids=['system', 'no-system'])
@pytest.mark.parametrize(
'distro', ['distro', None], ids=['distro', 'no-distro'])
@pytest.mark.parametrize(
'cla', ['class', None], ids=['class', 'no-class'])
@pytest.mark.parametrize(
'host', ['hostname', None], ids=['hostname', 'no-host'])
@pytest.mark.parametrize(
'user', ['user', None], ids=['user', 'no-user'])
def test_score_values(
runner, yadm, default, arch, system, distro, cla, host, user):
@pytest.mark.parametrize("default", ["default", None], ids=["default", "no-default"])
@pytest.mark.parametrize("arch", ["arch", None], ids=["arch", "no-arch"])
@pytest.mark.parametrize("system", ["system", None], ids=["system", "no-system"])
@pytest.mark.parametrize("distro", ["distro", None], ids=["distro", "no-distro"])
@pytest.mark.parametrize("cla", ["class", None], ids=["class", "no-class"])
@pytest.mark.parametrize("host", ["hostname", None], ids=["hostname", "no-host"])
@pytest.mark.parametrize("user", ["user", None], ids=["user", "no-user"])
def test_score_values(runner, yadm, default, arch, system, distro, cla, host, user):
"""Test score results"""
# pylint: disable=too-many-branches
local_class = 'testclass'
local_arch = 'testarch'
local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost'
local_user = 'testuser'
filenames = {'filename##': 0}
local_class = "testClass"
local_arch = "testARch"
local_system = "TESTsystem"
local_distro = "testDISTro"
local_host = "testHost"
local_user = "testUser"
filenames = {"filename##": 0}
if default:
for filename in list(filenames):
for label in CONDITION[default]['labels']:
for label in CONDITION[default]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
if not newfile.endswith("##"):
newfile += ","
newfile += label
filenames[newfile] = calculate_score(newfile)
if arch:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[arch]['labels']:
for label in CONDITION[arch]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_arch if match else 'badarch'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_arch if match else "badarch"])
filenames[newfile] = calculate_score(newfile)
if system:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[system]['labels']:
for label in CONDITION[system]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_system if match else 'badsys'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_system if match else "badsys"])
filenames[newfile] = calculate_score(newfile)
if distro:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[distro]['labels']:
for label in CONDITION[distro]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_distro if match else 'baddistro'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_distro if match else "baddistro"])
filenames[newfile] = calculate_score(newfile)
if cla:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[cla]['labels']:
for label in CONDITION[cla]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_class if match else 'badclass'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_class if match else "badclass"])
filenames[newfile] = calculate_score(newfile)
if host:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[host]['labels']:
for label in CONDITION[host]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_host if match else 'badhost'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_host if match else "badhost"])
filenames[newfile] = calculate_score(newfile)
if user:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[user]['labels']:
for label in CONDITION[user]["labels"]:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_user if match else 'baduser'
])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, local_user if match else "baduser"])
filenames[newfile] = calculate_score(newfile)
script = f"""
@ -212,29 +187,29 @@ def test_score_values(
local_host={local_host}
local_user={local_user}
"""
expected = ''
for filename in filenames:
expected = ""
for filename, score in filenames.items():
script += f"""
score_file "{filename}"
score_file "{filename}" "dest"
echo "{filename}"
echo "$score"
"""
expected += filename + '\n'
expected += str(filenames[filename]) + '\n'
run = runner(command=['bash'], inp=script)
expected += filename + "\n"
expected += str(score) + "\n"
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out == expected
@pytest.mark.parametrize('ext', [None, 'e', 'extension'])
@pytest.mark.parametrize("ext", [None, "e", "extension"])
def test_extensions(runner, yadm, ext):
"""Verify extensions do not effect scores"""
local_user = 'testuser'
filename = f'filename##u.{local_user}'
local_user = "testuser"
filename = f"filename##u.{local_user}"
if ext:
filename += f',{ext}.xyz'
expected = ''
filename += f",{ext}.xyz"
expected = ""
script = f"""
YADM_TEST=1 source {yadm}
score=0
@ -243,28 +218,28 @@ def test_extensions(runner, yadm, ext):
echo "$score"
"""
expected = f'{1000 + CONDITION["user"]["modifier"]}\n'
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out == expected
def test_score_values_templates(runner, yadm):
"""Test score results"""
local_class = 'testclass'
local_arch = 'arch'
local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost'
local_user = 'testuser'
filenames = {'filename##': 0}
local_class = "testclass"
local_arch = "arch"
local_system = "testsystem"
local_distro = "testdistro"
local_host = "testhost"
local_user = "testuser"
filenames = {"filename##": 0}
for filename in list(filenames):
for label in TEMPLATE_LABELS:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([label, 'testtemplate'])
if not newfile.endswith("##"):
newfile += ","
newfile += ".".join([label, "testtemplate"])
filenames[newfile] = calculate_score(newfile)
script = f"""
@ -277,58 +252,55 @@ def test_score_values_templates(runner, yadm):
local_host={local_host}
local_user={local_user}
"""
expected = ''
for filename in filenames:
expected = ""
for filename, score in filenames.items():
script += f"""
score_file "{filename}"
score_file "{filename}" "dest"
echo "{filename}"
echo "$score"
"""
expected += filename + '\n'
expected += str(filenames[filename]) + '\n'
run = runner(command=['bash'], inp=script)
expected += filename + "\n"
expected += str(score) + "\n"
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out == expected
@pytest.mark.parametrize(
'cmd_generated',
[True, False],
ids=['supported-template', 'unsupported-template'])
def test_template_recording(runner, yadm, cmd_generated):
"""Template should be recorded if choose_template_cmd outputs a command"""
@pytest.mark.parametrize("processor_generated", [True, False], ids=["supported-template", "unsupported-template"])
def test_template_recording(runner, yadm, processor_generated):
"""Template should be recorded if choose_template_processor outputs a command"""
mock = 'function choose_template_cmd() { return; }'
expected = ''
if cmd_generated:
mock = 'function choose_template_cmd() { echo "test_cmd"; }'
expected = 'template recorded'
mock = "function choose_template_processor() { return; }"
expected = ""
if processor_generated:
mock = 'function choose_template_processor() { echo "test_processor"; }'
expected = "template recorded"
script = f"""
YADM_TEST=1 source {yadm}
function record_template() {{ echo "template recorded"; }}
function record_score() {{ [ -n "$4" ] && echo "template recorded"; }}
{mock}
score_file "testfile##template.kind"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out.rstrip() == expected
def test_underscores_in_distro_and_family(runner, yadm):
"""Test replacing spaces in distro / distro_family with underscores"""
local_distro = 'test distro'
local_distro_family = 'test family'
def test_underscores_and_upper_case_in_distro_and_family(runner, yadm):
"""Test replacing spaces with underscores and lowering case in distro / distro_family"""
local_distro = "test distro"
local_distro_family = "test family"
filenames = {
'filename##distro.test distro': 1004,
'filename##distro.test-distro': 0,
'filename##distro.test_distro': 1004,
'filename##distro_family.test family': 1008,
'filename##distro_family.test-family': 0,
'filename##distro_family.test_family': 1008,
}
"filename##distro.Test Distro": 1004,
"filename##distro.test-distro": 0,
"filename##distro.test_distro": 1004,
"filename##distro_family.test FAMILY": 1008,
"filename##distro_family.test-family": 0,
"filename##distro_family.test_family": 1008,
}
script = f"""
YADM_TEST=1 source {yadm}
@ -336,16 +308,89 @@ def test_underscores_in_distro_and_family(runner, yadm):
local_distro="{local_distro}"
local_distro_family="{local_distro_family}"
"""
expected = ''
for filename in filenames:
expected = ""
for filename, score in filenames.items():
script += f"""
score_file "{filename}"
echo "{filename}"
echo "$score"
"""
expected += filename + '\n'
expected += str(filenames[filename]) + '\n'
run = runner(command=['bash'], inp=script)
expected += filename + "\n"
expected += str(score) + "\n"
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert run.out == expected
def test_negative_class_condition(runner, yadm):
"""Test negative class condition: returns 0 when matching and proper score when not matching."""
script = f"""
YADM_TEST=1 source {yadm}
local_class="testclass"
local_classes=("testclass")
# 0
score=0
score_file "filename##~class.testclass" "dest"
echo "score: $score"
# 16
score=0
score_file "filename##~class.badclass" "dest"
echo "score2: $score"
# 16
score=0
score_file "filename##~c.badclass" "dest"
echo "score3: $score"
"""
run = runner(command=["bash"], inp=script)
assert run.success
output = run.out.strip().splitlines()
assert output[0] == "score: 0"
assert output[1] == "score2: 16"
assert output[2] == "score3: 16"
def test_negative_combined_conditions(runner, yadm):
"""Test negative conditions for multiple alt types: returns 0 when matching and proper score when not matching."""
script = f"""
YADM_TEST=1 source {yadm}
local_class="testclass"
local_classes=("testclass")
local_distro="testdistro"
# (0) + (0) = 0
score=0
score_file "filename##~class.testclass,~distro.testdistro" "dest"
echo "score: $score"
# (1000 + 16) + (1000 + 4) = 2020
score=0
score_file "filename##class.testclass,distro.testdistro" "dest"
echo "score2: $score"
# 0 (negated class condition)
score=0
score_file "filename##~class.badclass,~distro.testdistro" "dest"
echo "score3: $score"
# (1000 + 16) + (4) = 1020
score=0
score_file "filename##class.testclass,~distro.baddistro" "dest"
echo "score4: $score"
# (1000 + 16) + (16) = 1032
score=0
score_file "filename##class.testclass,~class.badclass" "dest"
echo "score5: $score"
"""
run = runner(command=["bash"], inp=script)
assert run.success
output = run.out.strip().splitlines()
assert output[0] == "score: 0"
assert output[1] == "score2: 2020"
assert output[2] == "score3: 0"
assert output[3] == "score4: 1020"
assert output[4] == "score5: 1032"

View file

@ -1,30 +1,37 @@
"""Unit tests: set_local_alt_values"""
import pytest
import utils
@pytest.mark.parametrize(
'override', [
"override",
[
False,
'class',
'arch',
'os',
'hostname',
'user',
],
"class",
"arch",
"os",
"hostname",
"user",
"distro",
"distro-family",
],
ids=[
'no-override',
'override-class',
'override-arch',
'override-os',
'override-hostname',
'override-user',
]
)
@pytest.mark.usefixtures('ds1_copy')
"no-override",
"override-class",
"override-arch",
"override-os",
"override-hostname",
"override-user",
"override-distro",
"override-distro-family",
],
)
@pytest.mark.usefixtures("ds1_copy")
def test_set_local_alt_values(
runner, yadm, paths, tst_arch, tst_sys, tst_host, tst_user, override):
"""Use issue_legacy_path_warning"""
runner, yadm, paths, tst_arch, tst_sys, tst_host, tst_user, tst_distro, tst_distro_family, override
):
"""Test handling of local alt values"""
script = f"""
YADM_TEST=1 source {yadm} &&
set_operating_system &&
@ -33,60 +40,34 @@ def test_set_local_alt_values(
echo "class='$local_class'"
echo "arch='$local_arch'"
echo "os='$local_system'"
echo "host='$local_host'"
echo "hostname='$local_host'"
echo "user='$local_user'"
"""
if override == 'class':
utils.set_local(paths, override, 'first')
utils.set_local(paths, override, 'override', add=True)
elif override:
utils.set_local(paths, override, 'override')
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
if override == 'class':
assert "class='override'" in run.out
else:
assert "class=''" in run.out
if override == 'arch':
assert "arch='override'" in run.out
else:
assert f"arch='{tst_arch}'" in run.out
if override == 'os':
assert "os='override'" in run.out
else:
assert f"os='{tst_sys}'" in run.out
if override == 'hostname':
assert "host='override'" in run.out
else:
assert f"host='{tst_host}'" in run.out
if override == 'user':
assert "user='override'" in run.out
else:
assert f"user='{tst_user}'" in run.out
def test_distro_and_family(runner, yadm):
"""Assert that local_distro/local_distro_family are set"""
script = f"""
YADM_TEST=1 source {yadm}
function config() {{ echo "$1"; }}
function query_distro() {{ echo "testdistro"; }}
function query_distro_family() {{ echo "testfamily"; }}
set_local_alt_values
echo "distro='$local_distro'"
echo "distro_family='$local_distro_family'"
echo "distro-family='$local_distro_family'"
"""
run = runner(command=['bash'], inp=script)
if override == "class":
utils.set_local(paths, override, "first")
utils.set_local(paths, override, "override", add=True)
elif override:
utils.set_local(paths, override, "override")
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert "distro='testdistro'" in run.out
assert "distro_family='testfamily'" in run.out
assert run.err == ""
default_values = {
"class": "",
"arch": tst_arch,
"os": tst_sys,
"hostname": tst_host,
"user": tst_user,
"distro": tst_distro,
"distro-family": tst_distro_family,
}
for key, value in default_values.items():
if key == override:
assert f"{key}='override'" in run.out
else:
assert f"{key}='{value}'" in run.out

View file

@ -4,25 +4,27 @@ import pytest
@pytest.mark.parametrize(
'proc_value, expected_os', [
('missing', 'uname'),
('has microsoft inside', 'WSL'), # case insensitive
('has Microsoft inside', 'WSL'), # case insensitive
('another value', 'uname'),
], ids=[
'/proc/version missing',
'/proc/version includes ms',
'/proc/version excludes Ms',
'another value',
])
def test_set_operating_system(
runner, paths, tst_sys, proc_value, expected_os):
"proc_value, expected_os",
[
("missing", "uname"),
("has microsoft inside", "WSL"), # case insensitive
("has Microsoft inside", "WSL"), # case insensitive
("another value", "uname"),
],
ids=[
"/proc/version missing",
"/proc/version includes ms",
"/proc/version excludes Ms",
"another value",
],
)
def test_set_operating_system(runner, paths, tst_sys, proc_value, expected_os):
"""Run set_operating_system and test result"""
# Normally /proc/version (set in PROC_VERSION) is inspected to identify
# WSL. During testing, we will override that value.
proc_version = paths.root.join('proc_version')
if proc_value != 'missing':
proc_version = paths.root.join("proc_version")
if proc_value != "missing":
proc_version.write(proc_value)
script = f"""
YADM_TEST=1 source {paths.pgm}
@ -30,9 +32,9 @@ def test_set_operating_system(
set_operating_system
echo $OPERATING_SYSTEM
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if expected_os == 'uname':
expected_os = tst_sys
assert run.err == ""
if expected_os == "uname":
expected_os = tst_sys if tst_sys != "WSL" else "Linux"
assert run.out.rstrip() == expected_os

View file

@ -1,27 +1,23 @@
"""Unit tests: set_yadm_dirs"""
import pytest
@pytest.mark.parametrize(
'condition', [
'basic',
'override',
'override_data',
'xdg_config_home',
'xdg_data_home'
],
)
"condition",
["basic", "override", "override_data", "xdg_config_home", "xdg_data_home"],
)
def test_set_yadm_dirs(runner, yadm, condition):
"""Test set_yadm_dirs"""
setup = ''
if condition == 'override':
setup = 'YADM_DIR=/override'
elif condition == 'override_data':
setup = 'YADM_DATA=/override'
elif condition == 'xdg_config_home':
setup = 'XDG_CONFIG_HOME=/xdg'
elif condition == 'xdg_data_home':
setup = 'XDG_DATA_HOME=/xdg'
setup = ""
if condition == "override":
setup = "YADM_DIR=/override"
elif condition == "override_data":
setup = "YADM_DATA=/override"
elif condition == "xdg_config_home":
setup = "XDG_CONFIG_HOME=/xdg"
elif condition == "xdg_data_home":
setup = "XDG_DATA_HOME=/xdg"
script = f"""
HOME=/testhome
YADM_TEST=1 source {yadm}
@ -32,17 +28,17 @@ def test_set_yadm_dirs(runner, yadm, condition):
echo "YADM_DIR=$YADM_DIR"
echo "YADM_DATA=$YADM_DATA"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if condition == 'basic':
assert 'YADM_DIR=/testhome/.config/yadm' in run.out
assert 'YADM_DATA=/testhome/.local/share/yadm' in run.out
elif condition == 'override':
assert 'YADM_DIR=/override' in run.out
elif condition == 'override_data':
assert 'YADM_DATA=/override' in run.out
elif condition == 'xdg_config_home':
assert 'YADM_DIR=/xdg/yadm' in run.out
elif condition == 'xdg_data_home':
assert 'YADM_DATA=/xdg/yadm' in run.out
assert run.err == ""
if condition == "basic":
assert "YADM_DIR=/testhome/.config/yadm" in run.out
assert "YADM_DATA=/testhome/.local/share/yadm" in run.out
elif condition == "override":
assert "YADM_DIR=/override" in run.out
elif condition == "override_data":
assert "YADM_DATA=/override" in run.out
elif condition == "xdg_config_home":
assert "YADM_DIR=/xdg/yadm" in run.out
elif condition == "xdg_data_home":
assert "YADM_DATA=/xdg/yadm" in run.out

View file

@ -1,4 +1,5 @@
"""Unit tests: template_default"""
import os
FILE_MODE = 0o754
@ -12,7 +13,8 @@ LOCAL_HOST = "default_Test+@-!^Host"
LOCAL_USER = "default_Test+@-!^User"
LOCAL_DISTRO = "default_Test+@-!^Distro"
LOCAL_DISTRO_FAMILY = "default_Test+@-!^Family"
TEMPLATE = f'''
ENV_VAR = "default_Test+@-!^Env"
TEMPLATE = f"""
start of template
default class = >{{{{yadm.class}}}}<
default arch = >{{{{yadm.arch}}}}<
@ -30,13 +32,16 @@ Included section from else
{{% if yadm.class == "wrongclass1" %}}
wrong class 1
{{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS}" %}}
{{% if yadm.class != "wronglcass" %}}
Included section from !=
{{% endif\t\t %}}
{{% if yadm.class == "{LOCAL_CLASS.lower()}" %}}
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
Multiple lines
{{% else %}}
Should not be included...
{{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS2}" %}}
{{% if yadm.class == "{LOCAL_CLASS2.upper()}" %}}
Included section for second class
{{% endif %}}
{{% if yadm.class == "wrongclass2" %}}
@ -45,7 +50,7 @@ wrong class 2
{{% if yadm.arch == "wrongarch1" %}}
wrong arch 1
{{% endif %}}
{{% if yadm.arch == "{LOCAL_ARCH}" %}}
{{% if yadm.arch == "{LOCAL_ARCH.title()}" %}}
Included section for arch = {{{{yadm.arch}}}} ({{{{yadm.arch}}}} repeated)
{{% endif %}}
{{% if yadm.arch == "wrongarch2" %}}
@ -54,7 +59,7 @@ wrong arch 2
{{% if yadm.os == "wrongos1" %}}
wrong os 1
{{% endif %}}
{{% if yadm.os == "{LOCAL_SYSTEM}" %}}
{{% if yadm.os == "{LOCAL_SYSTEM.lower()}" %}}
Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
{{% endif %}}
{{% if yadm.os == "wrongos2" %}}
@ -63,7 +68,7 @@ wrong os 2
{{% if yadm.hostname == "wronghost1" %}}
wrong host 1
{{% endif %}}
{{% if yadm.hostname == "{LOCAL_HOST}" %}}
{{% if yadm.hostname == "{LOCAL_HOST.upper()}" %}}
Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
{{% endif %}}
{{% if yadm.hostname == "wronghost2" %}}
@ -72,7 +77,7 @@ wrong host 2
{{% if yadm.user == "wronguser1" %}}
wrong user 1
{{% endif %}}
{{% if yadm.user == "{LOCAL_USER}" %}}
{{% if yadm.user == "{LOCAL_USER.title()}" %}}
Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated)
{{% endif %}}
{{% if yadm.user == "wronguser2" %}}
@ -81,7 +86,7 @@ wrong user 2
{{% if yadm.distro == "wrongdistro1" %}}
wrong distro 1
{{% endif %}}
{{% if yadm.distro == "{LOCAL_DISTRO}" %}}
{{% if yadm.distro == "{LOCAL_DISTRO.lower()}" %}}
Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
{{% endif %}}
{{% if yadm.distro == "wrongdistro2" %}}
@ -90,16 +95,23 @@ wrong distro 2
{{% if yadm.distro_family == "wrongfamily1" %}}
wrong family 1
{{% endif %}}
{{% if yadm.distro_family == "{LOCAL_DISTRO_FAMILY}" %}}
{{% if yadm.distro_family == "{LOCAL_DISTRO_FAMILY.upper()}" %}}
Included section for distro_family = \
{{{{yadm.distro_family}}}} ({{{{yadm.distro_family}}}} again)
{{% endif %}}
{{% if yadm.distro_family == "wrongfamily2" %}}
wrong family 2
{{% endif %}}
{{% if env.VAR == "{ENV_VAR.title()}" %}}
Included section for env.VAR = {{{{env.VAR}}}} ({{{{env.VAR}}}} again)
{{% endif %}}
{{% if env.VAR == "wrongenvvar" %}}
wrong env.VAR
{{% endif %}}
yadm.no_such_var="{{{{ yadm.no_such_var }}}}" and env.NO_SUCH_VAR="{{{{ env.NO_SUCH_VAR }}}}"
end of template
'''
EXPECTED = f'''
"""
EXPECTED = f"""
start of template
default class = >{LOCAL_CLASS}<
default arch = >{LOCAL_ARCH}<
@ -111,6 +123,7 @@ default distro_family = >{LOCAL_DISTRO_FAMILY}<
classes = >{LOCAL_CLASS2}
{LOCAL_CLASS}<
Included section from else
Included section from !=
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Multiple lines
Included section for second class
@ -121,52 +134,90 @@ Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
Included section for distro_family = \
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
Included section for env.VAR = {ENV_VAR} ({ENV_VAR} again)
yadm.no_such_var="" and env.NO_SUCH_VAR=""
end of template
'''
"""
INCLUDE_BASIC = 'basic\n'
INCLUDE_VARIABLES = '''\
included <{{ yadm.class }}> file
INCLUDE_BASIC = "basic\n"
INCLUDE_VARIABLES = """\
included <{{ yadm.class }}> file ({{yadm.filename}})
empty line above
'''
INCLUDE_NESTED = 'no newline at the end'
"""
INCLUDE_NESTED = "no newline at the end"
TEMPLATE_INCLUDE = '''\
TEMPLATE_INCLUDE = """\
The first line
{% include empty %}
An empty file removes the line above
{%include basic%}
{% include "./variables.{{ yadm.os }}" %}
{% include dir/nested %}
{%include ./basic%}
{% include "variables.{{ yadm.os }}" %}
{% include dir/nested %}
Include basic again:
{% include basic %}
'''
EXPECTED_INCLUDE = f'''\
"""
EXPECTED_INCLUDE = f"""\
The first line
An empty file removes the line above
basic
included <{LOCAL_CLASS}> file
included <{LOCAL_CLASS}> file (VARIABLES_FILENAME)
empty line above
no newline at the end
Include basic again:
basic
'''
"""
TEMPLATE_NESTED_IFS = """\
{% if yadm.user == "me" %}
print1
{% if yadm.user == "me" %}
print2
{% else %}
no print1
{% endif %}
{% else %}
{% if yadm.user == "me" %}
no print2
{% else %}
no print3
{% endif %}
{% endif %}
{% if yadm.user != "me" %}
no print4
{% if yadm.user == "me" %}
no print5
{% else %}
no print6
{% endif %}
{% else %}
{% if yadm.user == "me" %}
print3
{% else %}
no print7
{% endif %}
{% endif %}
"""
EXPECTED_NESTED_IFS = """\
print1
print2
print3
"""
def test_template_default(runner, yadm, tmpdir):
"""Test template_default"""
input_file = tmpdir.join('input')
input_file = tmpdir.join("input")
input_file.write(TEMPLATE, ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
# ensure overwrite works when file exists as read-only (there is some
# special processing when this is encountered because some environments do
# not properly overwrite read-only files)
output_file.write('existing')
output_file.write("existing")
output_file.chmod(0o400)
script = f"""
@ -180,11 +231,11 @@ def test_template_default(runner, yadm, tmpdir):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script, env={"VAR": ENV_VAR})
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read() == EXPECTED
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
@ -192,19 +243,19 @@ def test_template_default(runner, yadm, tmpdir):
def test_source(runner, yadm, tmpdir):
"""Test yadm.source"""
input_file = tmpdir.join('input')
input_file.write('{{yadm.source}}', ensure=True)
input_file = tmpdir.join("input")
input_file.write("{{yadm.source}}", ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
script = f"""
YADM_TEST=1 source {yadm}
set_awk
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read().strip() == str(input_file)
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
@ -212,51 +263,71 @@ def test_source(runner, yadm, tmpdir):
def test_include(runner, yadm, tmpdir):
"""Test include"""
empty_file = tmpdir.join('empty')
empty_file.write('', ensure=True)
empty_file = tmpdir.join("empty")
empty_file.write("", ensure=True)
basic_file = tmpdir.join('basic')
basic_file = tmpdir.join("basic")
basic_file.write(INCLUDE_BASIC)
variables_file = tmpdir.join(f'variables.{LOCAL_SYSTEM}')
variables_file = tmpdir.join(f"variables.{LOCAL_SYSTEM}")
variables_file.write(INCLUDE_VARIABLES)
nested_file = tmpdir.join('dir').join('nested')
nested_file = tmpdir.join("dir").join("nested")
nested_file.write(INCLUDE_NESTED, ensure=True)
input_file = tmpdir.join('input')
input_file = tmpdir.join("input")
input_file.write(TEMPLATE_INCLUDE)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
expected = EXPECTED_INCLUDE.replace("VARIABLES_FILENAME", str(variables_file))
script = f"""
YADM_TEST=1 source {yadm}
set_awk
local_class="{LOCAL_CLASS}"
local_system="{LOCAL_SYSTEM}"
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert output_file.read() == EXPECTED_INCLUDE
assert run.err == ""
assert output_file.read() == expected
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
def test_nested_ifs(runner, yadm, tmpdir):
"""Test nested if statements"""
input_file = tmpdir.join("input")
input_file.write(TEMPLATE_NESTED_IFS, ensure=True)
output_file = tmpdir.join("output")
script = f"""
YADM_TEST=1 source {yadm}
set_awk
local_user="me"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert output_file.read() == EXPECTED_NESTED_IFS
def test_env(runner, yadm, tmpdir):
"""Test env"""
input_file = tmpdir.join('input')
input_file.write('{{env.PWD}}', ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
input_file = tmpdir.join("input")
input_file.write("{{env.PWD}}", ensure=True)
output_file = tmpdir.join("output")
script = f"""
YADM_TEST=1 source {yadm}
set_awk
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert output_file.read().strip() == os.environ['PWD']
assert run.err == ""
assert output_file.read().strip() == os.environ["PWD"]

View file

@ -1,4 +1,5 @@
"""Unit tests: template_esh"""
import os
FILE_MODE = 0o754
@ -11,7 +12,7 @@ LOCAL_HOST = "esh_Test+@-!^Host"
LOCAL_USER = "esh_Test+@-!^User"
LOCAL_DISTRO = "esh_Test+@-!^Distro"
LOCAL_DISTRO_FAMILY = "esh_Test+@-!^Family"
TEMPLATE = f'''
TEMPLATE = f"""
start of template
esh class = ><%=$YADM_CLASS%><
esh arch = ><%=$YADM_ARCH%><
@ -90,8 +91,8 @@ Included esh section for distro_family = \
wrong family 2
<% fi -%>
end of template
'''
EXPECTED = f'''
"""
EXPECTED = f"""
start of template
esh class = >{LOCAL_CLASS}<
esh arch = >{LOCAL_ARCH}<
@ -111,21 +112,22 @@ Included esh section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
Included esh section for distro_family = \
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
end of template
'''
"""
def test_template_esh(runner, yadm, tmpdir):
"""Test processing by esh"""
# pylint: disable=duplicate-code
input_file = tmpdir.join('input')
input_file = tmpdir.join("input")
input_file.write(TEMPLATE, ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
# ensure overwrite works when file exists as read-only (there is some
# special processing when this is encountered because some environments do
# not properly overwrite read-only files)
output_file.write('existing')
output_file.write("existing")
output_file.chmod(0o400)
script = f"""
@ -138,11 +140,11 @@ def test_template_esh(runner, yadm, tmpdir):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_esh "{input_file}" "{output_file}"
template esh "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read().strip() == str(EXPECTED).strip()
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
@ -150,17 +152,17 @@ def test_template_esh(runner, yadm, tmpdir):
def test_source(runner, yadm, tmpdir):
"""Test YADM_SOURCE"""
input_file = tmpdir.join('input')
input_file.write('<%= $YADM_SOURCE %>', ensure=True)
input_file = tmpdir.join("input")
input_file.write("<%= $YADM_SOURCE %>", ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
script = f"""
YADM_TEST=1 source {yadm}
template_esh "{input_file}" "{output_file}"
template esh "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read().strip() == str(input_file)
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode

View file

@ -1,5 +1,7 @@
"""Unit tests: template_j2cli & template_envtpl"""
import os
import pytest
FILE_MODE = 0o754
@ -12,7 +14,7 @@ LOCAL_HOST = "j2_Test+@-!^Host"
LOCAL_USER = "j2_Test+@-!^User"
LOCAL_DISTRO = "j2_Test+@-!^Distro"
LOCAL_DISTRO_FAMILY = "j2_Test+@-!^Family"
TEMPLATE = f'''
TEMPLATE = f"""
start of template
j2 class = >{{{{YADM_CLASS}}}}<
j2 arch = >{{{{YADM_ARCH}}}}<
@ -93,8 +95,8 @@ Included j2 section for distro_family = \
wrong family 2
{{%- endif %}}
end of template
'''
EXPECTED = f'''
"""
EXPECTED = f"""
start of template
j2 class = >{LOCAL_CLASS}<
j2 arch = >{LOCAL_ARCH}<
@ -115,22 +117,23 @@ Included j2 section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
Included j2 section for distro_family = \
{LOCAL_DISTRO_FAMILY} ({LOCAL_DISTRO_FAMILY} again)
end of template
'''
"""
@pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
@pytest.mark.parametrize("processor", ("j2cli", "envtpl"))
def test_template_j2(runner, yadm, tmpdir, processor):
"""Test processing by j2cli & envtpl"""
# pylint: disable=duplicate-code
input_file = tmpdir.join('input')
input_file = tmpdir.join("input")
input_file.write(TEMPLATE, ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
# ensure overwrite works when file exists as read-only (there is some
# special processing when this is encountered because some environments do
# not properly overwrite read-only files)
output_file.write('existing')
output_file.write("existing")
output_file.chmod(0o400)
script = f"""
@ -143,30 +146,30 @@ def test_template_j2(runner, yadm, tmpdir, processor):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_{processor} "{input_file}" "{output_file}"
template {processor} "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read() == EXPECTED
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
@pytest.mark.parametrize('processor', ('j2cli', 'envtpl'))
@pytest.mark.parametrize("processor", ("j2cli", "envtpl"))
def test_source(runner, yadm, tmpdir, processor):
"""Test YADM_SOURCE"""
input_file = tmpdir.join('input')
input_file.write('{{YADM_SOURCE}}', ensure=True)
input_file = tmpdir.join("input")
input_file.write("{{YADM_SOURCE}}", ensure=True)
input_file.chmod(FILE_MODE)
output_file = tmpdir.join('output')
output_file = tmpdir.join("output")
script = f"""
YADM_TEST=1 source {yadm}
template_{processor} "{input_file}" "{output_file}"
template {processor} "{input_file}" "{output_file}"
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
assert run.err == ""
assert output_file.read().strip() == str(input_file)
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode

View file

@ -1,22 +1,23 @@
"""Unit tests: upgrade"""
import pytest
@pytest.mark.parametrize('condition', ['override', 'equal', 'existing_repo'])
@pytest.mark.parametrize("condition", ["override", "equal", "existing_repo"])
def test_upgrade_errors(tmpdir, runner, yadm, condition):
"""Test upgrade() error conditions"""
home = tmpdir.mkdir('home')
yadm_dir = home.join('.config/yadm')
yadm_data = home.join('.local/share/yadm')
override = ''
if condition == 'override':
override = 'override'
if condition == 'equal':
home = tmpdir.mkdir("home")
yadm_dir = home.join(".config/yadm")
yadm_data = home.join(".local/share/yadm")
override = ""
if condition == "override":
override = "override"
if condition == "equal":
yadm_data = yadm_dir
if condition == 'existing_repo':
yadm_dir.ensure_dir('repo.git')
yadm_data.ensure_dir('repo.git')
if condition == "existing_repo":
yadm_dir.ensure_dir("repo.git")
yadm_data.ensure_dir("repo.git")
script = f"""
YADM_TEST=1 source {yadm}
@ -27,17 +28,16 @@ def test_upgrade_errors(tmpdir, runner, yadm, condition):
YADM_OVERRIDE_REPO="{override}"
upgrade
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.failure
assert 'Unable to upgrade' in run.err
if condition in ['override', 'equal']:
assert 'Paths have been overridden' in run.err
elif condition == 'existing_repo':
assert 'already exists' in run.err
assert "Unable to upgrade" in run.err
if condition in ["override", "equal"]:
assert "Paths have been overridden" in run.err
elif condition == "existing_repo":
assert "already exists" in run.err
@pytest.mark.parametrize(
'condition', ['no-paths', 'untracked', 'tracked', 'submodules'])
@pytest.mark.parametrize("condition", ["no-paths", "untracked", "tracked", "submodules"])
def test_upgrade(tmpdir, runner, yadm, condition):
"""Test upgrade()
@ -45,33 +45,32 @@ def test_upgrade(tmpdir, runner, yadm, condition):
mock for git. echo will return true, simulating a positive result from "git
ls-files". Also echo will report the parameters for "git mv".
"""
legacy_paths = ('config', 'encrypt', 'bootstrap', 'hooks/pre_cmd')
home = tmpdir.mkdir('home')
yadm_dir = home.join('.config/yadm')
yadm_data = home.join('.local/share/yadm')
yadm_legacy = home.join('.yadm')
legacy_paths = ("config", "encrypt", "bootstrap", "hooks/pre_cmd")
home = tmpdir.mkdir("home")
yadm_dir = home.join(".config/yadm")
yadm_data = home.join(".local/share/yadm")
yadm_legacy = home.join(".yadm")
if condition != 'no-paths':
yadm_dir.join('repo.git/config').write('test-repo', ensure=True)
yadm_dir.join('files.gpg').write('files.gpg', ensure=True)
if condition != "no-paths":
yadm_dir.join("repo.git/config").write("test-repo", ensure=True)
yadm_dir.join("files.gpg").write("files.gpg", ensure=True)
for path in legacy_paths:
yadm_legacy.join(path).write(path, ensure=True)
mock_git = ""
if condition != 'no-paths':
mock_git = f'''
if condition != "no-paths":
mock_git = f"""
function git() {{
echo "$@"
if [[ "$*" = *"submodule status" ]]; then
{ 'echo " 1234567 mymodule (1.0)"'
if condition == 'submodules' else ':' }
{'echo " 1234567 mymodule (1.0)"' if condition == 'submodules' else ':'}
fi
if [[ "$*" = *ls-files* ]]; then
return { 1 if condition == 'untracked' else 0 }
return {1 if condition == 'untracked' else 0}
fi
return 0
}}
'''
"""
script = f"""
YADM_TEST=1 source {yadm}
@ -85,38 +84,30 @@ def test_upgrade(tmpdir, runner, yadm, condition):
function cd {{ echo "$@";}}
upgrade
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ''
if condition == 'no-paths':
assert 'Upgrade is not necessary' in run.out
assert run.err == ""
if condition == "no-paths":
assert "Upgrade is not necessary" in run.out
else:
for (lpath, npath) in [
('repo.git', 'repo.git'), ('files.gpg', 'archive')]:
expected = (
f'Moving {yadm_dir.join(lpath)} '
f'to {yadm_data.join(npath)}')
for lpath, npath in [("repo.git", "repo.git"), ("files.gpg", "archive")]:
expected = f"Moving {yadm_dir.join(lpath)} " f"to {yadm_data.join(npath)}"
assert expected in run.out
for path in legacy_paths:
expected = (
f'Moving {yadm_legacy.join(path)} '
f'to {yadm_dir.join(path)}')
expected = f"Moving {yadm_legacy.join(path)} " f"to {yadm_dir.join(path)}"
assert expected in run.out
if condition == 'untracked':
assert 'test-repo' in yadm_data.join('repo.git/config').read()
assert 'files.gpg' in yadm_data.join('archive').read()
if condition == "untracked":
assert "test-repo" in yadm_data.join("repo.git/config").read()
assert "files.gpg" in yadm_data.join("archive").read()
for path in legacy_paths:
assert path in yadm_dir.join(path).read()
elif condition in ['tracked', 'submodules']:
expected = (
f'mv {yadm_dir.join("files.gpg")} '
f'{yadm_data.join("archive")}')
elif condition in ["tracked", "submodules"]:
expected = f'mv {yadm_dir.join("files.gpg")} ' f'{yadm_data.join("archive")}'
assert expected in run.out
assert 'files tracked by yadm have been renamed' in run.out
if condition == 'submodules':
assert 'submodule deinit -- mymodule' in run.out
assert 'submodule update --init --recursive -- mymodule' \
in run.out
assert "files tracked by yadm have been renamed" in run.out
if condition == "submodules":
assert "submodule deinit -- mymodule" in run.out
assert "submodule update --init --recursive -- mymodule" in run.out
else:
assert 'submodule deinit' not in run.out
assert 'submodule update --init --recursive' not in run.out
assert "submodule deinit" not in run.out
assert "submodule update --init --recursive" not in run.out

View file

@ -1,28 +1,30 @@
"""Unit tests: yadm.[git,gpg]-program"""
import os
import pytest
@pytest.mark.parametrize(
'executable, success, value, match', [
(None, True, 'program', None),
('cat', True, 'cat', None),
('badprogram', False, None, 'badprogram'),
], ids=[
'executable missing',
'valid alternative',
'invalid alternative',
])
@pytest.mark.parametrize('program', ['git', 'gpg'])
def test_x_program(
runner, yadm_cmd, paths, program, executable, success, value, match):
"executable, success, value, match",
[
(None, True, "program", None),
("cat", True, "cat", None),
("badprogram", False, None, "badprogram"),
],
ids=[
"executable missing",
"valid alternative",
"invalid alternative",
],
)
@pytest.mark.parametrize("program", ["git", "gpg"])
def test_x_program(runner, yadm_cmd, paths, program, executable, success, value, match):
"""Set yadm.X-program, and test result of require_X"""
# set configuration
if executable:
os.system(' '.join(yadm_cmd(
'config', f'yadm.{program}-program', executable)))
os.system(" ".join(yadm_cmd("config", f"yadm.{program}-program", executable)))
# test require_[git,gpg]
script = f"""
@ -32,11 +34,11 @@ def test_x_program(
require_{program}
echo ${program.upper()}_PROGRAM
"""
run = runner(command=['bash'], inp=script)
run = runner(command=["bash"], inp=script)
assert run.success == success
# [GIT,GPG]_PROGRAM set correctly
if value == 'program':
if value == "program":
assert run.out.rstrip() == program
elif value:
assert run.out.rstrip() == value
@ -45,4 +47,4 @@ def test_x_program(
if match:
assert match in run.err
else:
assert run.err == ''
assert run.err == ""

View file

@ -1,129 +1,133 @@
"""Test upgrade"""
import os
import pytest
@pytest.mark.parametrize(
'versions', [
('1.12.0', '2.5.0'),
('1.12.0',),
('2.5.0',),
], ids=[
'1.12.0 -> 2.5.0 -> latest',
'1.12.0 -> latest',
'2.5.0 -> latest',
])
@pytest.mark.parametrize(
'submodule', [False, True],
ids=['no submodule', 'with submodules'])
def test_upgrade(tmpdir, runner, versions, submodule):
"versions",
[
("1.12.0", "2.5.0"),
("1.12.0",),
("2.5.0",),
],
ids=[
"1.12.0 -> 2.5.0 -> latest",
"1.12.0 -> latest",
"2.5.0 -> latest",
],
)
@pytest.mark.parametrize("submodule", [False, True], ids=["no submodule", "with submodules"])
def test_upgrade(tmpdir, runner, paths, versions, submodule):
"""Upgrade tests"""
# pylint: disable=too-many-statements
home = tmpdir.mkdir('HOME')
env = {'HOME': str(home)}
home = tmpdir.mkdir("HOME")
env = {"HOME": str(home)}
runner(["git", "config", "--global", "init.defaultBranch", "master"], env=env)
runner(["git", "config", "--global", "protocol.file.allow", "always"], env=env)
runner(["git", "config", "--global", "user.email", "test@yadm.io"], env=env)
runner(["git", "config", "--global", "user.name", "Yadm Test"], env=env)
if submodule:
ext_repo = tmpdir.mkdir('ext_repo')
ext_repo.join('afile').write('some data')
ext_repo = tmpdir.mkdir("ext_repo")
ext_repo.join("afile").write("some data")
for cmd in (('init',), ('add', 'afile'), ('commit', '-m', 'test')):
run = runner(['git', '-C', str(ext_repo), *cmd])
for cmd in (("init",), ("add", "afile"), ("commit", "-m", "test")):
run = runner(["git", "-C", str(ext_repo), *cmd])
assert run.success
os.environ.pop('XDG_CONFIG_HOME', None)
os.environ.pop('XDG_DATA_HOME', None)
os.environ.pop("XDG_CONFIG_HOME", None)
os.environ.pop("XDG_DATA_HOME", None)
def run_version(version, *args, check_stderr=True):
yadm = 'yadm-%s' % version if version else '/yadm/yadm'
yadm = f"yadm-{version}" if version else paths.pgm
run = runner([yadm, *args], shell=True, cwd=str(home), env=env)
assert run.success
if check_stderr:
assert run.err == ''
assert run.err == ""
return run
# Initialize the repo with the first version
first = versions[0]
run_version(first, 'init')
run_version(first, "init")
home.join('file').write('some data')
run_version(first, 'add', 'file')
run_version(first, 'commit', '-m', '"First commit"')
home.join("file").write("some data")
run_version(first, "add", "file")
run_version(first, "commit", "-m", '"First commit"')
if submodule:
# When upgrading via 2.5.0 we can't have a submodule that's been added
# after being cloned as 2.5.0 fails the upgrade in that case.
can_upgrade_cloned_submodule = '2.5.0' not in versions[1:]
can_upgrade_cloned_submodule = "2.5.0" not in versions[1:]
if can_upgrade_cloned_submodule:
# Check out a repo and then add it as a submodule
run = runner(['git', '-C', str(home), 'clone', str(ext_repo), 'b'])
run = runner(["git", "-C", str(home), "clone", str(ext_repo), "b"])
assert run.success
run_version(first, 'submodule', 'add', str(ext_repo), 'b')
run_version(first, "submodule", "add", str(ext_repo), "b")
# Add submodule without first checking it out
run_version(first, 'submodule', 'add', str(ext_repo), 'a',
check_stderr=False)
run_version(first, 'submodule', 'add', str(ext_repo), 'c',
check_stderr=False)
run_version(first, "submodule", "add", str(ext_repo), "a", check_stderr=False)
run_version(first, "submodule", "add", str(ext_repo), "c", check_stderr=False)
run_version(first, 'commit', '-m', '"Add submodules"')
run_version(first, "commit", "-m", '"Add submodules"')
for path in ('.yadm', '.config/yadm'):
for path in (".yadm", ".config/yadm"):
yadm_dir = home.join(path)
if yadm_dir.exists():
break
yadm_dir.join('bootstrap').write('init stuff')
run_version(first, 'add', yadm_dir.join('bootstrap'))
run_version(first, 'commit', '-m', 'bootstrap')
yadm_dir.join("bootstrap").write("init stuff")
run_version(first, "add", yadm_dir.join("bootstrap"))
run_version(first, "commit", "-m", "bootstrap")
yadm_dir.join('encrypt').write('secret')
yadm_dir.join("encrypt").write("secret")
hooks_dir = yadm_dir.mkdir('hooks')
hooks_dir.join('pre_status').write('status')
hooks_dir.join('post_commit').write('commit')
hooks_dir = yadm_dir.mkdir("hooks")
hooks_dir.join("pre_status").write("status")
hooks_dir.join("post_commit").write("commit")
run_version(first, 'config', 'local.class', 'test')
run_version(first, 'config', 'foo.bar', 'true')
run_version(first, "config", "local.class", "test")
run_version(first, "config", "foo.bar", "true")
# Run upgrade with intermediate versions and latest
latest = None
for version in versions[1:] + (latest,):
run = run_version(version, 'upgrade', check_stderr=not submodule)
run = run_version(version, "upgrade", check_stderr=not submodule)
if submodule:
lines = run.err.splitlines()
if can_upgrade_cloned_submodule:
assert 'Migrating git directory of' in lines[0]
assert str(home.join('b/.git')) in lines[1]
assert str(yadm_dir.join('repo.git/modules/b')) in lines[2]
assert "Migrating git directory of" in lines[0]
assert str(home.join("b/.git")) in lines[1]
assert str(yadm_dir.join("repo.git/modules/b")) in lines[2]
del lines[:3]
for line in lines:
assert line.startswith('Submodule')
assert 'registered for path' in line
assert line.startswith("Submodule")
assert "registered for path" in line
# Verify result for the final upgrade
run_version(latest, 'status')
run_version(latest, "status")
run = run_version(latest, 'show', 'HEAD:file')
assert run.out == 'some data'
run = run_version(latest, "show", "HEAD:file")
assert run.out == "some data"
if submodule:
if can_upgrade_cloned_submodule:
assert home.join('b/afile').read() == 'some data'
assert home.join('a/afile').read() == 'some data'
assert home.join('c/afile').read() == 'some data'
assert home.join("b/afile").read() == "some data"
assert home.join("a/afile").read() == "some data"
assert home.join("c/afile").read() == "some data"
yadm_dir = home.join('.config/yadm')
yadm_dir = home.join(".config/yadm")
assert yadm_dir.join('bootstrap').read() == 'init stuff'
assert yadm_dir.join('encrypt').read() == 'secret'
assert yadm_dir.join("bootstrap").read() == "init stuff"
assert yadm_dir.join("encrypt").read() == "secret"
hooks_dir = yadm_dir.join('hooks')
assert hooks_dir.join('pre_status').read() == 'status'
assert hooks_dir.join('post_commit').read() == 'commit'
hooks_dir = yadm_dir.join("hooks")
assert hooks_dir.join("pre_status").read() == "status"
assert hooks_dir.join("post_commit").read() == "commit"
run = run_version(latest, 'config', 'local.class')
assert run.out.rstrip() == 'test'
run = run_version(latest, "config", "local.class")
assert run.out.rstrip() == "test"
run = run_version(latest, 'config', 'foo.bar')
assert run.out.rstrip() == 'true'
run = run_version(latest, "config", "foo.bar")
assert run.out.rstrip() == "true"

View file

@ -1,38 +1,36 @@
"""Test version"""
import re
import pytest
@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def expected_version(yadm):
"""
Expected semantic version number. This is taken directly out of yadm,
searching for the VERSION= string.
"""
yadm_version = re.findall(
r'VERSION=([^\n]+)',
open(yadm).read())
with open(yadm, encoding="utf-8") as source_file:
yadm_version = re.findall(r"VERSION=([^\n]+)", source_file.read())
if yadm_version:
return yadm_version[0]
pytest.fail(f'version not found in {yadm}')
return 'not found'
pytest.fail(f"version not found in {yadm}")
return "not found"
def test_semantic_version(expected_version):
"""Version is semantic"""
# semantic version conforms to MAJOR.MINOR.PATCH
assert re.search(r'^\d+\.\d+\.\d+$', expected_version), (
'does not conform to MAJOR.MINOR.PATCH')
assert re.search(r"^\d+\.\d+\.\d+$", expected_version), "does not conform to MAJOR.MINOR.PATCH"
@pytest.mark.parametrize('cmd', ['--version', 'version'])
def test_reported_version(
runner, yadm_cmd, cmd, expected_version):
@pytest.mark.parametrize("cmd", ["--version", "version"])
def test_reported_version(runner, yadm_cmd, cmd, expected_version):
"""Report correct version and bash/git versions"""
run = runner(command=yadm_cmd(cmd))
assert run.success
assert run.err == ''
assert 'bash version' in run.out
assert 'git version' in run.out
assert run.out.endswith(f'\nyadm version {expected_version}\n')
assert run.err == ""
assert "bash version" in run.out
assert "git version" in run.out
assert run.out.endswith(f"\nyadm version {expected_version}\n")

View file

@ -3,38 +3,42 @@
This module holds values/functions common to multiple tests.
"""
import re
import os
import re
ALT_FILE1 = 'test_alt'
ALT_FILE2 = 'test alt/test alt'
ALT_DIR = 'test alt/test alt dir'
ALT_FILE1 = "test_alt"
ALT_FILE2 = "test alt/test alt"
ALT_DIR = "test alt/test alt dir"
# Directory based alternates must have a tracked contained file.
# This will be the test contained file name
CONTAINED = 'contained_file'
CONTAINED = "contained_dir/contained_file"
# These variables are used for making include files which will be processed
# within jinja templates
INCLUDE_FILE = 'inc_file'
INCLUDE_DIRS = ['', 'test alt']
INCLUDE_CONTENT = '8780846c02e34c930d0afd127906668f'
INCLUDE_FILE = "inc_file"
INCLUDE_DIRS = ["", "test alt"]
INCLUDE_CONTENT = "8780846c02e34c930d0afd127906668f"
def set_local(paths, variable, value, add=False):
"""Set local override"""
add = "--add" if add else ""
os.system(
f'GIT_DIR={str(paths.repo)} '
f'git config --local {add} "local.{variable}" "{value}"'
)
os.system(f"GIT_DIR={str(paths.repo)} " f'git config --local {add} "local.{variable}" "{value}"')
def create_alt_files(paths, suffix,
preserve=False, tracked=True,
encrypt=False, exclude=False,
content=None, includefile=False,
yadm_alt=False, yadm_dir=None):
def create_alt_files(
paths,
suffix,
preserve=False,
tracked=True,
encrypt=False,
exclude=False,
content=None,
includefile=False,
yadm_alt=False,
yadm_dir=None,
):
"""Create new files, and add to the repo
This is used for testing alternate files. In each case, a suffix is
@ -42,7 +46,7 @@ def create_alt_files(paths, suffix,
repo handling are dependent upon the function arguments.
"""
basepath = yadm_dir.join('alt') if yadm_alt else paths.work
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
if not preserve:
for remove_path in (ALT_FILE1, ALT_FILE2, ALT_DIR):
@ -60,28 +64,28 @@ def create_alt_files(paths, suffix,
# Do not test directory support for jinja alternates
test_paths = [new_file1, new_file2]
test_names = [ALT_FILE1, ALT_FILE2]
if not re.match(r'##(t$|t\.|template|yadm)', suffix):
if not re.match(r"##(t$|t\.|template|yadm)", suffix):
test_paths += [new_dir]
test_names += [ALT_DIR]
for test_path in test_paths:
if content:
test_path.write('\n' + content, mode='a', ensure=True)
test_path.write("\n" + content, mode="a", ensure=True)
assert test_path.exists()
_create_includefiles(includefile, test_paths, basepath)
_create_tracked(tracked, test_paths, paths)
prefix = '.config/yadm/alt/' if yadm_alt else ''
prefix = ".config/yadm/alt/" if yadm_alt else ""
_create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix)
def parse_alt_output(output, linked=True):
"""Parse output of 'alt', and return list of linked files"""
regex = r'Creating (.+) from template (.+)$'
regex = r"Creating (.+) from template (.+)$"
if linked:
regex = r'Linking (.+) to (.+)$'
parsed_list = dict()
regex = r"(?:Copy|Link)ing (.+) to (.+)$"
parsed_list = {}
for line in output.splitlines():
match = re.match(regex, line)
if match:
@ -95,7 +99,7 @@ def parse_alt_output(output, linked=True):
def _create_includefiles(includefile, test_paths, basepath):
if includefile:
for dpath in INCLUDE_DIRS:
incfile = basepath.join(dpath + '/' + INCLUDE_FILE)
incfile = basepath.join(dpath + "/" + INCLUDE_FILE)
incfile.write(INCLUDE_CONTENT, ensure=True)
test_paths += [incfile]
@ -110,8 +114,6 @@ def _create_tracked(tracked, test_paths, paths):
def _create_encrypt(encrypt, test_names, suffix, paths, exclude, prefix):
if encrypt:
for encrypt_name in test_names:
paths.encrypt.write(
f'{prefix + encrypt_name + suffix}\n', mode='a')
paths.encrypt.write(f"{prefix + encrypt_name + suffix}\n", mode="a")
if exclude:
paths.encrypt.write(
f'!{prefix + encrypt_name + suffix}\n', mode='a')
paths.encrypt.write(f"!{prefix + encrypt_name + suffix}\n", mode="a")

1375
yadm

File diff suppressed because it is too large Load diff

290
yadm.1
View file

@ -1,5 +1,5 @@
.\" vim: set spell so=8:
.TH yadm 1 "23 January 2023" "3.2.2"
.TH YADM 1 "March 3, 2025" "3.5.0"
.SH NAME
@ -15,55 +15,55 @@ yadm \- Yet Another Dotfiles Manager
.I git-command-or-alias
.RI [ options ]
.B yadm
init
.RB [ -f ]
.RB [ -w
.B yadm init
.RB [ \-f ]
.RB [ \-w
.IR dir ]
.B yadm
.RI clone " url
.RB [ -f ]
.RB [ -w
.B yadm clone
.I url
.RB [ \-f ]
.RB [ \-w
.IR dir ]
.RB [ -b
.RB [ \-b
.IR branch ]
.RB [ --bootstrap ]
.RB [ --no-bootstrap ]
.RB [ \-\-bootstrap ]
.RB [ \-\-no\-bootstrap ]
.B yadm
.RI config " name
.B yadm config
.I name
.RI [ value ]
.B yadm
config
.RB [ -e ]
.B yadm config
.RB [ \-e ]
.B yadm
list
.RB [ -a ]
.B yadm list
.RB [ \-a ]
.BR yadm " bootstrap
.B yadm bootstrap
.BR yadm " encrypt
.B yadm encrypt
.BR yadm " decrypt
.RB [ -l ]
.B yadm decrypt
.RB [ \-l ]
.BR yadm " alt
.B yadm alt
.BR yadm " perms
.B yadm perms
.BR yadm " enter [ command ]
.B yadm enter
.RI [ command ]
.BR yadm " git-crypt [ options ]
.B yadm git\-crypt
.RI [ options ]
.BR yadm " transcrypt [ options ]
.B yadm transcrypt
.RI [ options ]
.BR yadm " upgrade
.RB [ -f ]
.B yadm upgrade
.RB [ \-f ]
.BR yadm " introspect
.B yadm introspect
.I category
.SH DESCRIPTION
@ -83,8 +83,7 @@ Any command not internally handled by yadm is passed through to
.BR git (1).
Git commands or aliases are invoked with the yadm managed repository.
The working directory for Git commands will be the configured
.IR work-tree " (usually
.IR $HOME ).
.IR work-tree \ (usually\ $HOME ).
Dotfiles are managed by using standard
.B git
@ -95,7 +94,7 @@ commands;
.IR pull ,
etc.
.RI The " config
.RI The\ config
command is not passed directly through.
Instead use the
.I gitconfig
@ -114,7 +113,7 @@ Execute
.I $HOME/.config/yadm/bootstrap
if it exists.
.TP
.BI clone " url
.BI clone \ url
Clone a remote repository for tracking dotfiles.
After the contents of the remote repository have been fetched, a "check out" of
the remote HEAD branch is attempted.
@ -130,15 +129,15 @@ By default,
will be used as the
.IR work-tree ,
but this can be overridden with the
.BR -w " option.
.BR \-w \ option.
yadm can be forced to overwrite an existing repository by providing the
.BR -f " option.
.BR \-f \ option.
If you want to use a branch other than the remote HEAD branch
you can specify it using the
.BR -b " option.
.BR \-b \ option.
By default yadm will ask the user if the bootstrap program should be run (if it
exists). The options
.BR --bootstrap " or " --no-bootstrap
.BR \-\-bootstrap " or " \-\-no\-bootstrap
will either force the bootstrap to be run, or prevent it from being run,
without prompting the user.
.TP
@ -153,10 +152,9 @@ See the CONFIGURATION section for more details.
Decrypt all files stored in
.IR $HOME/.local/share/yadm/archive .
Files decrypted will be relative to the configured
.IR work-tree " (usually
.IR $HOME ).
.IR work-tree \ (usually\ $HOME ).
Using the
.B -l
.B \-l
option will list the files stored without extracting them.
.TP
.B encrypt
@ -191,12 +189,12 @@ Emacs Tramp and Magit can manage files by using this configuration:
With this config, use (magit-status "/yadm::").
.RE
.TP
.BI git-crypt " options
.BI git-crypt \ options
If git-crypt is installed, this command allows you to pass options directly to
git-crypt, with the environment configured to use the yadm repository.
git-crypt enables transparent encryption and decryption of files in a git repository.
You can read
git-crypt enables transparent encryption and decryption of files in a git
repository. You can read
https://github.com/AGWA/git-crypt
for details.
.TP
@ -232,17 +230,17 @@ By default,
will be used as the
.IR work-tree ,
but this can be overridden with the
.BR -w " option.
.BR \-w \ option.
yadm can be forced to overwrite an existing repository by providing the
.BR -f " option.
.BR \-f \ option.
.TP
.B list
Print a list of files managed by yadm.
.RB The " -a
.RB The \ \-a
option will cause all managed files to be listed.
Otherwise, the list will only include files from the current directory or below.
.TP
.BI introspect " category
.BI introspect \ category
Report internal yadm data. Supported categories are
.IR commands ,
.IR configs ,
@ -259,12 +257,12 @@ configuration
.I yadm.auto-perms
to "false".
.TP
.BI transcrypt " options
.BI transcrypt \ options
If transcrypt is installed, this command allows you to pass options directly to
transcrypt, with the environment configured to use the yadm repository.
transcrypt enables transparent encryption and decryption of files in a git repository.
You can read
transcrypt enables transparent encryption and decryption of files in a git
repository. You can read
https://github.com/elasticdog/transcrypt
for details.
.TP
@ -283,7 +281,7 @@ your submodules cannot be de-initialized, the upgrade will fail. The most
common reason submodules will fail to de-initialize is because they have local
modifications. If you are willing to lose the local modifications to those
submodules, you can use the
.B -f
.B \-f
option with the "upgrade" command to force the de-initialization.
After running "yadm upgrade", you should run "yadm status" to review changes
@ -306,33 +304,33 @@ For example, the following alias could be used to override the repository
directory.
.RS
alias yadm='yadm --yadm-repo /alternate/path/to/repo'
alias yadm='yadm \-\-yadm\-repo /alternate/path/to/repo'
.RE
The following is the full list of universal options.
Each option should be followed by a path.
.TP
.B -Y,--yadm-dir
.B \-Y, \-\-yadm\-dir
Override the yadm directory.
yadm stores its configurations relative to this directory.
.TP
.B --yadm-data
.B \-\-yadm\-data
Override the yadm data directory.
yadm stores its data relative to this directory.
.TP
.B --yadm-repo
.B \-\-yadm\-repo
Override the location of the yadm repository.
.TP
.B --yadm-config
.B \-\-yadm\-config
Override the location of the yadm configuration file.
.TP
.B --yadm-encrypt
.B \-\-yadm\-encrypt
Override the location of the yadm encryption configuration.
.TP
.B --yadm-archive
.B \-\-yadm\-archive
Override the location of the yadm encrypted files archive.
.TP
.B --yadm-bootstrap
.B \-\-yadm\-bootstrap
Override the location of the yadm bootstrap program.
.SH CONFIGURATION
@ -365,7 +363,8 @@ you may still run "yadm alt" manually to create the alternate links. This
feature is enabled by default.
.TP
.B yadm.auto-exclude
Disable the automatic exclusion of patterns defined in
Disable the automatic exclusion of created alternate links, template files and
patterns defined in
.IR $HOME/.config/yadm/encrypt .
This feature is enabled by default.
.TP
@ -377,7 +376,8 @@ manually to update permissions.
This feature is enabled by default.
.TP
.B yadm.auto-private-dirs
Disable the automatic creating of private directories described in the section PERMISSIONS.
Disable the automatic creating of private directories described in the section
PERMISSIONS.
.TP
.B yadm.cipher
Configure which encryption system is used by the encrypt/decrypt commands.
@ -426,8 +426,8 @@ Disable the permission changes to
.IR $HOME/.ssh/* .
This feature is enabled by default.
.RE
The following five "local" configurations are not stored in the
.LP
The following "local" configurations are not stored in the
.IR $HOME/.config/yadm/config,
they are stored in the local repository.
@ -438,7 +438,7 @@ By default, no class will be matched.
The local host can be assigned multiple classes using command:
.RS
yadm config --add local.class <additional-class>
yadm config \-\-add local.class <additional-class>
.RE
.TP
.B local.arch
@ -452,6 +452,12 @@ Override the OS for the purpose of symlinking alternate files.
.TP
.B local.user
Override the user for the purpose of symlinking alternate files.
.TP
.B local.distro
Override the distro for the purpose of symlinking alternate files.
.TP
.B local.distro-family
Override the distro family for the purpose of symlinking alternate files.
.SH ALTERNATES
@ -470,29 +476,34 @@ commas.
Each condition is an attribute/value pair, separated by a period. Some
conditions do not require a "value", and in that case, the period and value can
be omitted. Most attributes can be abbreviated as a single letter.
be omitted. Most attributes can be abbreviated as a single letter. Prefixing an
attribute with "~" negates the condition, meaning the condition is considered
only if the attribute/value pair evaluates to false.
<attribute>[.<value>]
[~]<attribute>[.<value>]
.BR NOTE :
Value is compared case-insensitive.
These are the supported attributes, in the order of the weighted precedence:
.TP
.BR template , " t
.BR template ,\ t
Valid when the value matches a supported template processor.
See the TEMPLATES section for more details.
.TP
.BR user , " u
.BR user ,\ u
Valid if the value matches the current user.
Current user is calculated by running
.BR "id -u -n" .
.BR "id \-u \-n" .
.TP
.BR hostname , " h
.BR hostname ,\ h
Valid if the value matches the short hostname.
Hostname is calculated by running
.BR "uname -n" ,
.BR "uname \-n" ,
and trimming off any domain.
.TP
.BR class , " c
.BR class ,\ c
Valid if the value matches the
.B local.class
configuration.
@ -501,37 +512,38 @@ Class must be manually set using
See the CONFIGURATION section for more details about setting
.BR local.class .
.TP
.BR distro , " d
.BR distro_family ,\ f
Valid if the value matches the distro family.
Distro family is calculated by inspecting the ID_LIKE line from
.B "/etc/os-release"
(or ID if no ID_LIKE line is found).
.TP
.BR distro ,\ d
Valid if the value matches the distro.
Distro is calculated by running
.B "lsb_release -si"
.B "lsb_release \-si"
or by inspecting the ID from
.BR "/etc/os-release" .
.TP
.BR distro_family , " f
Valid if the value matches the distro family.
Distro family is calculated by inspecting the ID_LIKE line from
.BR "/etc/os-release" .
.TP
.BR os , " o
.BR os ,\ o
Valid if the value matches the OS.
OS is calculated by running
.BR "uname -s" .
.BR "uname \-s" .
.TP
.BR arch , " a
.BR arch ,\ a
Valid if the value matches the architecture.
Architecture is calculated by running
.BR "uname -m" .
.BR "uname \-m" .
.TP
.B default
Valid when no other alternate is valid.
.TP
.BR extension , " e
.BR extension ,\ e
A special "condition" that doesn't affect the selection process. Its purpose is
instead to allow the alternate file to end with a certain extension to
e.g. make editors highlight the content properly.
.LP
.LP
.BR NOTE :
The OS for "Windows Subsystem for Linux" is reported as "WSL", even
though uname identifies as "Linux".
@ -545,9 +557,10 @@ symbolic links will be created for the most appropriate version.
The "most appropriate" version is determined by calculating a score for each
version of a file. A template is always scored higher than any symlink
condition. The number of conditions is the next largest factor in scoring.
Files with more conditions will always be favored. Any invalid condition will
disqualify that file completely.
condition. The number of conditions is the next largest factor in scoring;
files with more conditions will always be favored. Negative conditions (prefixed
with "~") are scored only relative to the number of non-negated conditions.
Any invalid condition will disqualify that file completely.
If you don't care to have all versions of alternates stored in the same
directory as the generated symlink, you can place them in the
@ -560,6 +573,7 @@ files are managed by yadm's repository:
- $HOME/path/example.txt##default
- $HOME/path/example.txt##class.Work
- $HOME/path/example.txt##class.Work,~os.Darwin
- $HOME/path/example.txt##os.Darwin
- $HOME/path/example.txt##os.Darwin,hostname.host1
- $HOME/path/example.txt##os.Darwin,hostname.host2
@ -577,7 +591,8 @@ which looks like this:
.IR $HOME/path/example.txt " -> " $HOME/path/example.txt##os.Darwin
Since the hostname doesn't match any of the managed files, the more generic version is chosen.
Since the hostname doesn't match any of the managed files, the more generic
version is chosen.
If running on a Linux server named "host4", the link will be:
@ -587,15 +602,22 @@ If running on a Solaris server, the link will use the default version:
.IR $HOME/path/example.txt " -> " $HOME/path/example.txt##default
If running on a system, with class set to "Work", the link will be:
If running on a Macbook with class set to "Work", the link will be:
.IR $HOME/path/example.txt " -> " $HOME/path/example.txt##class.Work
Since class has higher precedence than os, this version is chosen.
If running on a system with class set to "Work", but instead within Windows
Subsystem for Linux, where the os is reported as WSL, the link will be:
.IR $HOME/path/example.txt " -> " $HOME/path/example.txt##class.Work,~os.Darwin
If no "##default" version exists and no files have valid conditions, then no
link will be created.
Links are also created for directories named this way, as long as they have at
least one yadm managed file within them (at the top level).
least one yadm managed file within them.
yadm will automatically create these links by default. This can be disabled
using the
@ -604,6 +626,12 @@ configuration.
Even if disabled, links can be manually created by running
.BR "yadm alt" .
Created links are automatically added to the repository's
.I info/exclude
file. This can be disabled using the
.I yadm.auto-exclude
configuration.
Class is a special value which is stored locally on each host (inside the local
repository). To use alternate symlinks using class, you must set the value of
class using the configuration
@ -614,13 +642,15 @@ command. The following sets the class to be "Work".
yadm config local.class Work
Similarly, the values of architecture, os, hostname, and user can be manually
overridden using the configuration options
Similarly, the values of architecture, os, hostname, user, distro, and
distro_family can be manually overridden using the configuration options
.BR local.arch ,
.BR local.os ,
.BR local.hostname ,
.BR local.user ,
.BR local.distro ,
and
.BR local.user .
.BR local.distro-family .
.SH TEMPLATES
@ -637,6 +667,9 @@ upon
.BR awk ,
which is available on most *nix systems. To use this processor,
specify the value of "default" or just leave the value off (e.g. "##template").
.BR NOTE :
This template processor performs case-insensitive comparisions in if statements.
.TP
.B ESH
ESH is a template processor written in POSIX compliant shell. It allows
@ -652,9 +685,10 @@ To use the j2cli Jinja template processor, specify the value of "j2" or
"j2cli".
.TP
.B envtpl
To use the envtpl Jinja template processor, specify the value of "j2" or "envtpl".
.LP
To use the envtpl Jinja template processor, specify the value of "j2" or
"envtpl".
.LP
.BR NOTE :
Specifying "j2" as the processor will attempt to use j2cli or envtpl, whichever
is available.
@ -666,15 +700,16 @@ During processing, the following variables are available in the template:
Default Jinja or ESH Description
------------- ------------- ----------------------------
yadm.arch YADM_ARCH uname -m
yadm.arch YADM_ARCH uname \-m
yadm.class YADM_CLASS Last locally defined class
yadm.classes YADM_CLASSES All classes
yadm.distro YADM_DISTRO lsb_release -si
yadm.distro YADM_DISTRO lsb_release \-si
yadm.distro_family YADM_DISTRO_FAMILY ID_LIKE from /etc/os-release
yadm.hostname YADM_HOSTNAME uname -n (without domain)
yadm.os YADM_OS uname -s
yadm.filename Filename for the current file
yadm.hostname YADM_HOSTNAME uname \-n (without domain)
yadm.os YADM_OS uname \-s
yadm.source YADM_SOURCE Template filename
yadm.user YADM_USER id -u -n
yadm.user YADM_USER id \-u \-n
env.VAR Environment variable VAR
.BR NOTE :
@ -732,6 +767,12 @@ would look like:
<%+ whatever.extra %>
<% fi -%>
Created files are automatically added to the repository's
.I info/exclude
file. This can be disabled using the
.I yadm.auto-exclude
configuration.
.SH ENCRYPTION
It can be useful to manage confidential files, like SSH or GPG keys, across
@ -749,11 +790,11 @@ gpg is used by default, but openssl can be configured with the
.I yadm.cipher
configuration.
To use this feature, a list of patterns must be created and saved as
To use this feature, a list of patterns (one per line) must be created and
saved as
.IR $HOME/.config/yadm/encrypt .
This list of patterns should be relative to the configured
.IR work-tree " (usually
.IR $HOME ).
.IR work-tree \ (usually\ $HOME ).
For example:
.RS
@ -761,20 +802,20 @@ For example:
.gnupg/*.gpg
.RE
Standard filename expansions (*, ?, [) are supported.
If you have Bash version 4, you may use "**" to match all subdirectories.
Other shell expansions like brace and tilde are not supported.
Spaces in paths are supported, and should not be quoted.
If a directory is specified, its contents will be included, but not recursively.
Standard filename expansions (*, ?, [) are supported. Two consecutive asterisks
"**" can be used to match all subdirectories. Other shell expansions like
brace and tilde are not supported. Spaces in paths are supported, and should
not be quoted. If a directory is specified, its contents will be included.
Paths beginning with a "!" will be excluded.
The
.B yadm encrypt
command will find all files matching the patterns, and prompt for a password. Once a
password has confirmed, the matching files will be encrypted and saved as
command will find all files matching the patterns, and prompt for a
password. Once a password has confirmed, the matching files will be encrypted
and saved as
.IR $HOME/.local/share/yadm/archive .
The "encrypt" and "archive" files should be added to the yadm repository so they are
available across multiple systems.
The "encrypt" and "archive" files should be added to the yadm repository so
they are available across multiple systems.
To decrypt these files later, or on another system run
.B yadm decrypt
@ -817,7 +858,6 @@ repository. See the following web sites for more information:
- https://github.com/elasticdog/transcrypt
- https://github.com/AGWA/git-crypt
.LP
.SH PERMISSIONS
@ -826,7 +866,7 @@ dependent upon the user's umask. Because of this, yadm will automatically
update the permissions of some file paths. The "group" and "others" permissions
will be removed from the following files:
.RI - " $HOME/.local/share/yadm/archive
.RI -\ $HOME/.local/share/yadm/archive
- All files matching patterns in
.I $HOME/.config/yadm/encrypt
@ -898,6 +938,12 @@ Hooks have the following environment variables available to them at runtime:
.B YADM_HOOK_COMMAND
The command which triggered the hook
.TP
.B YADM_HOOK_DATA
The path to the yadm data directory
.TP
.B YADM_HOOK_DIR
The path to the yadm directory
.TP
.B YADM_HOOK_EXIT
The exit status of the yadm command
.TP
@ -976,7 +1022,7 @@ to the Git index and create a new commit
.B yadm remote add origin <url>
Add a remote origin to an existing repository
.TP
.B yadm push -u origin master
.B yadm push \-u origin master
Initial push of master to origin
.TP
.B echo ".ssh/*.key" >> $HOME/.config/yadm/encrypt
@ -989,12 +1035,14 @@ Commit a new set of encrypted files
Report issues or create pull requests at GitHub:
https://github.com/TheLocehiliosan/yadm/issues
https://github.com/yadm-dev/yadm/issues
.SH AUTHOR
.SH AUTHORS
Tim Byrne <sultan@locehilios.com>
Erik Flodin <erik@flodin.me>
.SH SEE ALSO
.BR git (1),

317
yadm.md
View file

@ -1,6 +1,4 @@
## NAME
yadm - Yet Another Dotfiles Manager
@ -30,11 +28,11 @@
yadm perms
yadm enter [ command ]
yadm enter [command]
yadm git-crypt [ options ]
yadm git-crypt [options]
yadm transcrypt [ options ]
yadm transcrypt [options]
yadm upgrade [-f]
@ -56,17 +54,17 @@
aged repository. The working directory for Git commands will be
the configured work-tree (usually $HOME).
Dotfiles are managed by using standard git commands; add, com
Dotfiles are managed by using standard git commands; add, com
mit, push, pull, etc.
The config command is not passed directly through. Instead use
The config command is not passed directly through. Instead use
the gitconfig command (see below).
alt Create symbolic links and process templates for any managed
files matching the naming rules described in the ALTERNATES and
TEMPLATES sections. It is usually unnecessary to run this com
mand, as yadm automatically processes alternates by default.
This automatic behavior can be disabled by setting the configu
alt Create symbolic links and process templates for any managed
files matching the naming rules described in the ALTERNATES and
TEMPLATES sections. It is usually unnecessary to run this com
mand, as yadm automatically processes alternates by default.
This automatic behavior can be disabled by setting the configu
ration yadm.auto-alt to "false".
bootstrap
@ -82,12 +80,12 @@
The repository is stored in $HOME/.local/share/yadm/repo.git.
By default, $HOME will be used as the work-tree, but this can be
overridden with the -w option. yadm can be forced to overwrite
an existing repository by providing the -f option. If you want
to use a branch other than the remote HEAD branch you can spec
ify it using the -b option. By default yadm will ask the user
if the bootstrap program should be run (if it exists). The op
tions --bootstrap or --no-bootstrap will either force the boot
overridden with the -w option. yadm can be forced to overwrite
an existing repository by providing the -f option. If you want
to use a branch other than the remote HEAD branch you can spec
ify it using the -b option. By default yadm will ask the user
if the bootstrap program should be run (if it exists). The op
tions --bootstrap or --no-bootstrap will either force the boot
strap to be run, or prevent it from being run, without prompting
the user.
@ -96,27 +94,27 @@
section for more details.
decrypt
Decrypt all files stored in $HOME/.local/share/yadm/archive.
Files decrypted will be relative to the configured work-tree
(usually $HOME). Using the -l option will list the files stored
without extracting them.
Decrypt all files stored in $HOME/.local/share/yadm/archive.
Files decrypted will be relative to the configured work-
tree (usually $HOME). Using the -l option will list the files
stored without extracting them.
encrypt
Encrypt all files matching the patterns found in $HOME/.con
Encrypt all files matching the patterns found in $HOME/.con
fig/yadm/encrypt. See the ENCRYPTION section for more details.
enter Run a sub-shell with all Git variables set. Exit the sub-shell
the same way you leave your normal shell (usually with the
"exit" command). This sub-shell can be used to easily interact
with your yadm repository using "git" commands. This could be
useful if you are using a tool which uses Git directly, such as
enter Run a sub-shell with all Git variables set. Exit the sub-shell
the same way you leave your normal shell (usually with the
"exit" command). This sub-shell can be used to easily interact
with your yadm repository using "git" commands. This could be
useful if you are using a tool which uses Git directly, such as
tig, vim-fugitive, git-cola, etc.
Optionally, you can provide a command after "enter", and instead
of invoking your shell, that command will be run with all of the
Git variables exposed to the command's environment.
Emacs Tramp and Magit can manage files by using this configura
Emacs Tramp and Magit can manage files by using this configura
tion:
(add-to-list 'tramp-methods
@ -130,20 +128,20 @@
With this config, use (magit-status "/yadm::").
git-crypt options
If git-crypt is installed, this command allows you to pass op
tions directly to git-crypt, with the environment configured to
If git-crypt is installed, this command allows you to pass op
tions directly to git-crypt, with the environment configured to
use the yadm repository.
git-crypt enables transparent encryption and decryption of files
in a git repository. You can read https://github.com/AGWA/git-
in a git repository. You can read https://github.com/AGWA/git-
crypt for details.
gitconfig
Pass options to the git config command. Since yadm already uses
the config command to manage its own configurations, this com
mand is provided as a way to change configurations of the repos
itory managed by yadm. One useful case might be to configure
the repository so untracked files are shown in status commands.
itory managed by yadm. One useful case might be to configure
the repository so untracked files are shown in status commands.
yadm initially configures its repository so that untracked files
are not shown. If you wish use the default Git behavior (to
show untracked files and directories), you can remove this con
@ -160,11 +158,11 @@
an existing repository by providing the -f option.
list Print a list of files managed by yadm. The -a option will cause
all managed files to be listed. Otherwise, the list will only
all managed files to be listed. Otherwise, the list will only
include files from the current directory or below.
introspect category
Report internal yadm data. Supported categories are commands,
Report internal yadm data. Supported categories are commands,
configs, repo, and switches. The purpose of introspection is to
support command line completion.
@ -179,24 +177,24 @@
tions directly to transcrypt, with the environment configured to
use the yadm repository.
transcrypt enables transparent encryption and decryption of
files in a git repository. You can read
https://github.com/elasticdog/transcrypt for details.
transcrypt enables transparent encryption and decryption of
files in a git repository. You can read https://github.com/elas
ticdog/transcrypt for details.
upgrade
Version 3 of yadm uses a different directory for storing data.
When you start to use version 3 for the first time, you may see
warnings about moving your data to this new directory. The eas
iest way to accomplish this is by running "yadm upgrade". This
command will start by moving your yadm repo to the new path.
Next it will move any archive data. If the archive is tracked
iest way to accomplish this is by running "yadm upgrade". This
command will start by moving your yadm repo to the new path.
Next it will move any archive data. If the archive is tracked
within your yadm repo, this command will "stage" the renaming of
that file in the repo's index.
Upgrading will attempt to de-initialize and re-initialize your
submodules. If your submodules cannot be de-initialized, the up
grade will fail. The most common reason submodules will fail to
de-initialize is because they have local modifications. If you
grade will fail. The most common reason submodules will fail to
de-initialize is because they have local modifications. If you
are willing to lose the local modifications to those submodules,
you can use the -f option with the "upgrade" command to force
the de-initialization.
@ -213,20 +211,20 @@
## OPTIONS
yadm supports a set of universal options that alter the paths it uses.
The default paths are documented in the FILES section. Any path speci
fied by these options must be fully qualified. If you always want to
override one or more of these paths, it may be useful to create an
alias for the yadm command. For example, the following alias could be
yadm supports a set of universal options that alter the paths it uses.
The default paths are documented in the FILES section. Any path speci
fied by these options must be fully qualified. If you always want to
override one or more of these paths, it may be useful to create an
alias for the yadm command. For example, the following alias could be
used to override the repository directory.
alias yadm='yadm --yadm-repo /alternate/path/to/repo'
The following is the full list of universal options. Each option
The following is the full list of universal options. Each option
should be followed by a path.
-Y,--yadm-dir
Override the yadm directory. yadm stores its configurations
-Y, --yadm-dir
Override the yadm directory. yadm stores its configurations
relative to this directory.
--yadm-data
@ -271,8 +269,9 @@
create the alternate links. This feature is enabled by default.
yadm.auto-exclude
Disable the automatic exclusion of patterns defined in
$HOME/.config/yadm/encrypt. This feature is enabled by default.
Disable the automatic exclusion of created alternate links, tem
plate files and patterns defined in $HOME/.config/yadm/encrypt.
This feature is enabled by default.
yadm.auto-perms
Disable the automatic permission changes described in the sec
@ -291,48 +290,49 @@
TION.
yadm.git-program
Specify an alternate program to use instead of "git". By de
Specify an alternate program to use instead of "git". By de
fault, the first "git" found in $PATH is used.
yadm.gpg-perms
Disable the permission changes to $HOME/.gnupg/*. This feature
Disable the permission changes to $HOME/.gnupg/*. This feature
is enabled by default.
yadm.gpg-program
Specify an alternate program to use instead of "gpg". By de
Specify an alternate program to use instead of "gpg". By de
fault, the first "gpg" found in $PATH is used.
yadm.gpg-recipient
Asymmetrically encrypt files with a gpg public/private key pair.
Provide a "key ID" to specify which public key to encrypt with.
The key must exist in your public keyrings. Multiple recipients
can be specified (separated by space). If left blank or not
provided, symmetric encryption is used instead. If set to
"ASK", gpg will interactively ask for recipients. See the EN
CRYPTION section for more details. This feature is disabled by
can be specified (separated by space). If left blank or not
provided, symmetric encryption is used instead. If set to
"ASK", gpg will interactively ask for recipients. See the EN
CRYPTION section for more details. This feature is disabled by
default.
yadm.openssl-ciphername
Specify which cipher should be used by openssl. "aes-256-cbc"
Specify which cipher should be used by openssl. "aes-256-cbc"
is used by default.
yadm.openssl-old
Newer versions of openssl support the pbkdf2 key derivation
function. This is used by default. If this configuration is set
to "true", openssl operations will use options compatible with
older versions of openssl. If you change this option, you will
Newer versions of openssl support the pbkdf2 key derivation
function. This is used by default. If this configuration is set
to "true", openssl operations will use options compatible with
older versions of openssl. If you change this option, you will
need to recreate your encrypted archive.
yadm.openssl-program
Specify an alternate program to use instead of "openssl". By
Specify an alternate program to use instead of "openssl". By
default, the first "openssl" found in $PATH is used.
yadm.ssh-perms
Disable the permission changes to $HOME/.ssh/*. This feature is
enabled by default.
The following five "local" configurations are not stored in the
$HOME/.config/yadm/config, they are stored in the local repository.
The following "local" configurations are not stored in the $HOME/.con
fig/yadm/config, they are stored in the local repository.
local.class
@ -343,11 +343,11 @@
yadm config --add local.class <additional-class>
local.arch
Override the architecture for the purpose of symlinking alter
Override the architecture for the purpose of symlinking alter
nate files.
local.hostname
Override the hostname for the purpose of symlinking alternate
Override the hostname for the purpose of symlinking alternate
files.
local.os
@ -356,15 +356,23 @@
local.user
Override the user for the purpose of symlinking alternate files.
local.distro
Override the distro for the purpose of symlinking alternate
files.
local.distro-family
Override the distro family for the purpose of symlinking alter
nate files.
## ALTERNATES
When managing a set of files across different systems, it can be useful
to have an automated way of choosing an alternate version of a file for
a different operating system, host, user, etc.
yadm will automatically create a symbolic link to the appropriate ver
sion of a file, when a valid suffix is appended to the filename. The
suffix contains the conditions that must be met for that file to be
yadm will automatically create a symbolic link to the appropriate ver
sion of a file, when a valid suffix is appended to the filename. The
suffix contains the conditions that must be met for that file to be
used.
The suffix begins with "##", followed by any number of conditions sepa
@ -375,20 +383,24 @@
Each condition is an attribute/value pair, separated by a period. Some
conditions do not require a "value", and in that case, the period and
value can be omitted. Most attributes can be abbreviated as a single
letter.
letter. Prefixing an attribute with "~" negates the condition, meaning
the condition is considered only if the attribute/value pair evaluates
to false.
<attribute>[.<value>]
[~]<attribute>[.<value>]
NOTE: Value is compared case-insensitive.
These are the supported attributes, in the order of the weighted prece
dence:
template, t
Valid when the value matches a supported template processor.
Valid when the value matches a supported template processor.
See the TEMPLATES section for more details.
user, u
Valid if the value matches the current user. Current user is
Valid if the value matches the current user. Current user is
calculated by running id -u -n.
hostname, h
@ -401,15 +413,16 @@
the CONFIGURATION section for more details about setting lo
cal.class.
distro_family, f
Valid if the value matches the distro family. Distro family is
calculated by inspecting the ID_LIKE line from /etc/os-release
(or ID if no ID_LIKE line is found).
distro, d
Valid if the value matches the distro. Distro is calculated by
running lsb_release -si or by inspecting the ID from /etc/os-re
lease.
distro_family, f
Valid if the value matches the distro family. Distro family is
calculated by inspecting the ID_LIKE line from /etc/os-release.
os, o Valid if the value matches the OS. OS is calculated by running
uname -s.
@ -426,6 +439,7 @@
certain extension to e.g. make editors highlight the content
properly.
NOTE: The OS for "Windows Subsystem for Linux" is reported as "WSL",
even though uname identifies as "Linux".
@ -436,21 +450,24 @@
most appropriate version.
The "most appropriate" version is determined by calculating a score for
each version of a file. A template is always scored higher than any
symlink condition. The number of conditions is the next largest factor
in scoring. Files with more conditions will always be favored. Any in
valid condition will disqualify that file completely.
each version of a file. A template is always scored higher than any
symlink condition. The number of conditions is the next largest factor
in scoring; files with more conditions will always be favored. Negative
conditions (prefixed with "~") are scored only relative to the number
of non-negated conditions. Any invalid condition will disqualify that
file completely.
If you don't care to have all versions of alternates stored in the same
directory as the generated symlink, you can place them in the
$HOME/.config/yadm/alt directory. The generated symlink or processed
$HOME/.config/yadm/alt directory. The generated symlink or processed
template will be created using the same relative path.
Alternate linking may best be demonstrated by example. Assume the fol
Alternate linking may best be demonstrated by example. Assume the fol
lowing files are managed by yadm's repository:
- $HOME/path/example.txt##default
- $HOME/path/example.txt##class.Work
- $HOME/path/example.txt##class.Work,~os.Darwin
- $HOME/path/example.txt##os.Darwin
- $HOME/path/example.txt##os.Darwin,hostname.host1
- $HOME/path/example.txt##os.Darwin,hostname.host2
@ -469,7 +486,7 @@
$HOME/path/example.txt -> $HOME/path/example.txt##os.Darwin
Since the hostname doesn't match any of the managed files, the more
Since the hostname doesn't match any of the managed files, the more
generic version is chosen.
If running on a Linux server named "host4", the link will be:
@ -480,35 +497,47 @@
$HOME/path/example.txt -> $HOME/path/example.txt##default
If running on a system, with class set to "Work", the link will be:
If running on a Macbook with class set to "Work", the link will be:
$HOME/path/example.txt -> $HOME/path/example.txt##class.Work
If no "##default" version exists and no files have valid conditions,
Since class has higher precedence than os, this version is chosen.
If running on a system with class set to "Work", but instead within
Windows Subsystem for Linux, where the os is reported as WSL, the link
will be:
$HOME/path/example.txt -> $HOME/path/example.txt##class.Work,~os.Darwin
If no "##default" version exists and no files have valid conditions,
then no link will be created.
Links are also created for directories named this way, as long as they
have at least one yadm managed file within them (at the top level).
Links are also created for directories named this way, as long as they
have at least one yadm managed file within them.
yadm will automatically create these links by default. This can be dis
abled using the yadm.auto-alt configuration. Even if disabled, links
can be manually created by running yadm alt.
Class is a special value which is stored locally on each host (inside
the local repository). To use alternate symlinks using class, you must
set the value of class using the configuration local.class. This is
Created links are automatically added to the repository's info/exclude
file. This can be disabled using the yadm.auto-exclude configuration.
Class is a special value which is stored locally on each host (inside
the local repository). To use alternate symlinks using class, you must
set the value of class using the configuration local.class. This is
set like any other yadm configuration with the yadm config command. The
following sets the class to be "Work".
yadm config local.class Work
Similarly, the values of architecture, os, hostname, and user can be
manually overridden using the configuration options local.arch, lo
cal.os, local.hostname, and local.user.
Similarly, the values of architecture, os, hostname, user, distro, and
distro_family can be manually overridden using the configuration op
tions local.arch, local.os, local.hostname, local.user, local.distro,
and local.distro-family.
## TEMPLATES
If a template condition is defined in an alternate file's "##" suffix,
If a template condition is defined in an alternate file's "##" suffix,
and the necessary dependencies for the template are available, then the
file will be processed to create or overwrite files.
@ -516,33 +545,37 @@
default
This is yadm's built-in template processor. This processor is
very basic, with a Jinja-like syntax. The advantage of this pro
cessor is that it only depends upon awk, which is available on
most *nix systems. To use this processor, specify the value of
"default" or just leave the value off (e.g. "##template").
very basic, with a Jinja-like syntax. The advantage of this
processor is that it only depends upon awk, which is available
on most *nix systems. To use this processor, specify the value
of "default" or just leave the value off (e.g. "##template").
NOTE: This template processor performs case-insensitive compari
sions in if statements.
ESH ESH is a template processor written in POSIX compliant shell. It
allows executing shell commands within templates. This can be
used to reference your own configurations within templates, for
allows executing shell commands within templates. This can be
used to reference your own configurations within templates, for
example:
<% yadm config mysection.myconfig %>
To use the ESH template processor, specify the value of "esh"
j2cli To use the j2cli Jinja template processor, specify the value of
j2cli To use the j2cli Jinja template processor, specify the value of
"j2" or "j2cli".
envtpl To use the envtpl Jinja template processor, specify the value of
"j2" or "envtpl".
NOTE: Specifying "j2" as the processor will attempt to use j2cli or en
vtpl, whichever is available.
If the template processor specified is available, templates will be
If the template processor specified is available, templates will be
processed to create or overwrite files.
During processing, the following variables are available in the tem
During processing, the following variables are available in the tem
plate:
Default Jinja or ESH Description
@ -552,6 +585,8 @@
yadm.classes YADM_CLASSES All classes
yadm.distro YADM_DISTRO lsb_release -si
yadm.distro_family YADM_DISTRO_FAMILY ID_LIKE from /etc/os-release
yadm.filename Filename for the current
file
yadm.hostname YADM_HOSTNAME uname -n (without domain)
yadm.os YADM_OS uname -s
yadm.source YADM_SOURCE Template filename
@ -605,35 +640,38 @@
<%+ whatever.extra %>
<% fi -%>
Created files are automatically added to the repository's info/exclude
file. This can be disabled using the yadm.auto-exclude configuration.
## ENCRYPTION
It can be useful to manage confidential files, like SSH or GPG keys,
across multiple systems. However, doing so would put plain text data
It can be useful to manage confidential files, like SSH or GPG keys,
across multiple systems. However, doing so would put plain text data
into a Git repository, which often resides on a public system. yadm can
make it easy to encrypt and decrypt a set of files so the encrypted
version can be maintained in the Git repository. This feature will
only work if a supported tool is available. Both gpg(1) and openssl(1)
are supported. gpg is used by default, but openssl can be configured
are supported. gpg is used by default, but openssl can be configured
with the yadm.cipher configuration.
To use this feature, a list of patterns must be created and saved as
$HOME/.config/yadm/encrypt. This list of patterns should be relative
to the configured work-tree (usually $HOME). For example:
To use this feature, a list of patterns (one per line) must be created
and saved as $HOME/.config/yadm/encrypt. This list of patterns should
be relative to the configured work-tree (usually $HOME). For example:
.ssh/*.key
.gnupg/*.gpg
Standard filename expansions (*, ?, [) are supported. If you have Bash
version 4, you may use "**" to match all subdirectories. Other shell
Standard filename expansions (*, ?, [) are supported. Two consecutive
asterisks "**" can be used to match all subdirectories. Other shell
expansions like brace and tilde are not supported. Spaces in paths are
supported, and should not be quoted. If a directory is specified, its
contents will be included, but not recursively. Paths beginning with a
"!" will be excluded.
contents will be included. Paths beginning with a "!" will be ex
cluded.
The yadm encrypt command will find all files matching the patterns, and
prompt for a password. Once a password has confirmed, the matching
files will be encrypted and saved as $HOME/.local/share/yadm/archive.
The "encrypt" and "archive" files should be added to the yadm reposi
prompt for a password. Once a password has confirmed, the matching
files will be encrypted and saved as $HOME/.local/share/yadm/archive.
The "encrypt" and "archive" files should be added to the yadm reposi
tory so they are available across multiple systems.
To decrypt these files later, or on another system run yadm decrypt and
@ -647,7 +685,7 @@
confidential files, even though they are encrypted.
Patterns found in $HOME/.config/yadm/encrypt are automatically added to
the repository's info/exclude file every time yadm encrypt is run.
the repository's info/exclude file every time yadm encrypt is run.
This is to prevent accidentally committing sensitive data to the repos
itory. This can be disabled using the yadm.auto-exclude configuration.
@ -663,6 +701,7 @@
- https://github.com/AGWA/git-crypt
## PERMISSIONS
When files are checked out of a Git repository, their initial permis
sions are dependent upon the user's umask. Because of this, yadm will
@ -678,19 +717,19 @@
- The GPG directory and files, .gnupg/*
yadm will automatically update permissions by default. This can be dis
abled using the yadm.auto-perms configuration. Even if disabled, per
missions can be manually updated by running yadm perms. The .ssh di
rectory processing can be disabled using the yadm.ssh-perms configura
tion. The .gnupg directory processing can be disabled using the
abled using the yadm.auto-perms configuration. Even if disabled, per
missions can be manually updated by running yadm perms. The .ssh di
rectory processing can be disabled using the yadm.ssh-perms configura
tion. The .gnupg directory processing can be disabled using the
yadm.gpg-perms configuration.
When cloning a repo which includes data in a .ssh or .gnupg directory,
if those directories do not exist at the time of cloning, yadm will
When cloning a repo which includes data in a .ssh or .gnupg directory,
if those directories do not exist at the time of cloning, yadm will
create the directories with mask 0700 prior to merging the fetched data
into the work-tree.
When running a Git command and .ssh or .gnupg directories do not exist,
yadm will create those directories with mask 0700 prior to running the
yadm will create those directories with mask 0700 prior to running the
Git command. This can be disabled using the yadm.auto-private-dirs con
figuration.
@ -716,6 +755,12 @@
YADM_HOOK_COMMAND
The command which triggered the hook
YADM_HOOK_DATA
The path to the yadm data directory
YADM_HOOK_DIR
The path to the yadm directory
YADM_HOOK_EXIT
The exit status of the yadm command
@ -739,8 +784,8 @@
Otherwise it will be $HOME/.config/yadm.
Similarly, yadm's data files are relative to the "yadm data directory".
yadm uses the "XDG Base Directory Specification" to determine this di
rectory. If the environment variable $XDG_DATA_HOME is defined as a
yadm uses the "XDG Base Directory Specification" to determine this di
rectory. If the environment variable $XDG_DATA_HOME is defined as a
fully qualified path, this directory will be $XDG_DATA_HOME/yadm. Oth
erwise it will be $HOME/.local/share/yadm.
@ -798,17 +843,17 @@
## REPORTING BUGS
Report issues or create pull requests at GitHub:
https://github.com/TheLocehiliosan/yadm/issues
https://github.com/yadm-dev/yadm/issues
## AUTHOR
## AUTHORS
Tim Byrne <sultan@locehilios.com>
Erik Flodin <erik@flodin.me>
## SEE ALSO
git(1), gpg(1) openssl(1) transcrypt(1) git-crypt(1)
https://yadm.io/

View file

@ -1,7 +1,7 @@
%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}
Name: yadm
Summary: Yet Another Dotfiles Manager
Version: 3.2.2
Version: 3.5.0
Group: Development/Tools
Release: 1%{?dist}
URL: https://yadm.io