Skip to content

Commit

Permalink
Add test-spice for easier development (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcalixte authored Jul 21, 2024
1 parent dfc82ad commit d7991c7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ To check if a spice with UUID satifies those requirements run the `validate-spic
./validate-spice UUID
```

## Development

To facilitate easier testing of Desklets locally, run the `test-spice` script in this repo:

Validate and then copy a Spice with UUID:

```bash
./test-spice UUID
```

Skip validation (not recommended) and then copy a Spice with UUID:

```bash
./test-spice -s UUID
```

Remove all locally installed development copies of Spices:

```bash
./test-spice -r
```

NOTE: Local copies of Spices for development/testing purposes will have a `devtest-` prefix attached for easier identification and cleanup.

# Rights and responsibility of the author

The author is in charge of the development of the spice.
Expand Down
82 changes: 82 additions & 0 deletions test-spice
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python3

import argparse
import json
import os
import sys
import shutil
import subprocess
from pathlib import Path

DEV_PREFIX = "devtest-"
DESKLETS_PATH = f'{Path.home()}/.local/share/cinnamon/desklets/'


def validate_spice(uuid):
"""
Run the validation for the Spice
"""
try:
out = subprocess.run(['./validate-spice', uuid], check=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if out.returncode != 0:
print(out.stdout.decode('ascii') + '\nValidation failed!')
except FileNotFoundError:
print('Errors encountered! Please try running again from the top-level'
' directory of the repository.')


def copy_xlet(uuid):
"""
Copy the UUID directory and action file for testing purposes
"""
uuid_path = f'{uuid}/files/{uuid}'
dev_dest = f'{DESKLETS_PATH}{DEV_PREFIX}{uuid}'

try:
shutil.copytree(uuid_path, dev_dest, dirs_exist_ok=True)
metadata_file = f'{dev_dest}/metadata.json'
with open(metadata_file, 'r+', encoding='utf-8') as metadata:
data = json.load(metadata)
metadata.seek(0)
data['uuid'] = f"{DEV_PREFIX}{data['uuid']}"
data['name'] = f"({DEV_PREFIX.replace('-', '')}) {data['name']}"
json.dump(data, metadata, indent=4)
except (FileNotFoundError, KeyError):
# metadata.json file not found or missing valid keys
pass


def main():
"""
Simpler testing of Spices for developers
"""
parser = argparse.ArgumentParser()
parser.description = 'Copy a Spice locally for testing purposes'
parser.add_argument('-r', '--remove', action='store_true',
help='Remove all test Spices')
parser.add_argument('-s', '--skip', action='store_true',
help='Skip Spice validation with validate-spice')
parser.add_argument('uuid', type=str, metavar='UUID', nargs='?',
help='the UUID of the Spice')
args = parser.parse_args()

if args.remove and not args.uuid:
for file_path in os.listdir(DESKLETS_PATH):
if file_path.startswith(DEV_PREFIX):
rm_path = f'{DESKLETS_PATH}{file_path}'
if Path.is_dir(Path(rm_path)):
shutil.rmtree(rm_path)
sys.exit(0)
elif args.skip and args.uuid:
copy_xlet(args.uuid.rstrip('/'))
elif args.uuid and not args.remove:
validate_spice(args.uuid.rstrip('/'))
copy_xlet(args.uuid.rstrip('/'))
else:
parser.print_help()
sys.exit(2)


if __name__ == "__main__":
main()

0 comments on commit d7991c7

Please sign in to comment.