mirror of
https://github.com/qmk/qmk_cli.git
synced 2026-08-04 00:06:35 -04:00
Add initial test suite
This commit is contained in:
+58
-14
@@ -1,24 +1,24 @@
|
||||
"""Helpers for working with git.
|
||||
"""
|
||||
"""Helpers for working with git."""
|
||||
|
||||
import subprocess
|
||||
from .helpers import rmtree
|
||||
|
||||
from milc import cli
|
||||
|
||||
default_repo = 'qmk_firmware'
|
||||
default_fork = 'qmk/' + default_repo
|
||||
default_branch = 'master'
|
||||
DEFAULT_UPSTREAM = 'https://github.com/qmk/qmk_firmware'
|
||||
|
||||
|
||||
def git_clone(url, destination, branch):
|
||||
"""Add the qmk/qmk_firmware upstream to a qmk_firmware clone."""
|
||||
git_clone = [
|
||||
'git',
|
||||
'clone',
|
||||
'--recurse-submodules',
|
||||
'--branch=' + branch,
|
||||
f'--branch={branch}',
|
||||
url,
|
||||
str(destination),
|
||||
]
|
||||
cli.log.debug('Git clone command: %s', git_clone)
|
||||
cli.log.debug(f'Git clone command: {git_clone}')
|
||||
|
||||
try:
|
||||
with subprocess.Popen(git_clone, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True, encoding='utf-8') as p:
|
||||
@@ -28,13 +28,57 @@ def git_clone(url, destination, branch):
|
||||
except Exception as e:
|
||||
git_cmd = ' '.join([s.replace(' ', r'\ ') for s in git_clone])
|
||||
|
||||
cli.log.error("Could not run '%s': %s: %s", git_cmd, e.__class__.__name__, e)
|
||||
cli.log.error(f"Could not run '{git_cmd}': {e.__class__.__name__}:{e}")
|
||||
return False
|
||||
|
||||
if p.returncode == 0:
|
||||
cli.log.info('Successfully cloned %s to %s!', url, destination)
|
||||
return True
|
||||
|
||||
else:
|
||||
cli.log.error('git clone exited %d', p.returncode)
|
||||
if p.returncode != 0:
|
||||
cli.log.error(f'git clone exited {p.returncode}')
|
||||
return False
|
||||
|
||||
cli.log.info(f'Successfully cloned {url} to {destination}!')
|
||||
return True
|
||||
|
||||
|
||||
def git_set_upstream(destination):
|
||||
"""Add the qmk/qmk_firmware upstream to a qmk_firmware clone."""
|
||||
git_remote = [
|
||||
'git',
|
||||
'-C',
|
||||
destination,
|
||||
'remote',
|
||||
'add',
|
||||
'upstream',
|
||||
DEFAULT_UPSTREAM,
|
||||
]
|
||||
cli.log.debug(f'Git remote command: {git_remote}')
|
||||
|
||||
try:
|
||||
with subprocess.Popen(git_remote, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True, encoding='utf-8') as p:
|
||||
for line in p.stdout:
|
||||
print(line, end='')
|
||||
|
||||
except Exception as e:
|
||||
git_cmd = ' '.join([s.replace(' ', r'\ ') for s in git_remote])
|
||||
|
||||
cli.log.error(f"Could not run '{git_cmd}': {e.__class__.__name__}:{e}")
|
||||
return False
|
||||
|
||||
if p.returncode != 0:
|
||||
cli.log.error(f'git remote add exited {p.returncode}')
|
||||
return False
|
||||
|
||||
cli.log.info(f'Added {DEFAULT_UPSTREAM} as remote upstream.')
|
||||
return True
|
||||
|
||||
|
||||
def git_clone_fork(destination, baseurl, fork, branch, force=False):
|
||||
"""Clone fork and configure upstream."""
|
||||
url = '/'.join((baseurl, fork))
|
||||
|
||||
if force:
|
||||
rmtree(destination)
|
||||
|
||||
if not git_clone(url, destination, branch):
|
||||
return False
|
||||
|
||||
return git_set_upstream(destination)
|
||||
|
||||
@@ -23,6 +23,6 @@ def clone(cli):
|
||||
# Exists (but not an empty dir)
|
||||
if cli.args.destination.exists() and any(cli.args.destination.iterdir()):
|
||||
cli.log.error('Destination already exists: %s', cli.args.destination)
|
||||
exit(1)
|
||||
return False
|
||||
|
||||
return git_clone(git_url, cli.args.destination, cli.args.branch)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Prints environment information.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from milc import cli
|
||||
@@ -26,6 +27,10 @@ def env(cli):
|
||||
data[converted_key] = val
|
||||
|
||||
if cli.args.var:
|
||||
if cli.args.var not in data:
|
||||
print(f'Variable "{cli.args.var}" does not exist!', file=sys.stderr)
|
||||
return False
|
||||
|
||||
# dump out requested arg
|
||||
print(data[cli.args.var])
|
||||
else:
|
||||
|
||||
@@ -2,64 +2,26 @@
|
||||
"""
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from milc import cli
|
||||
from milc.questions import choice, question, yesno
|
||||
from qmk_cli.git import git_clone
|
||||
from qmk_cli.helpers import AbsPath, is_qmk_firmware, rmtree
|
||||
from qmk_cli.git import git_clone_fork
|
||||
from qmk_cli.helpers import AbsPath, is_qmk_firmware
|
||||
|
||||
default_base = 'https://github.com'
|
||||
default_repo = 'qmk_firmware'
|
||||
default_fork = 'qmk/' + default_repo
|
||||
default_branch = 'master'
|
||||
|
||||
|
||||
def git_upstream(destination):
|
||||
"""Add the qmk/qmk_firmware upstream to a qmk_firmware clone.
|
||||
"""
|
||||
git_url = '/'.join((cli.args.baseurl, default_fork))
|
||||
git_cmd = [
|
||||
'git',
|
||||
'-C',
|
||||
destination,
|
||||
'remote',
|
||||
'add',
|
||||
'upstream',
|
||||
git_url,
|
||||
]
|
||||
|
||||
with subprocess.Popen(git_cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True, encoding='utf-8') as p:
|
||||
for line in p.stdout:
|
||||
print(line, end='')
|
||||
|
||||
if p.returncode == 0:
|
||||
cli.log.info('Added %s as remote upstream.', git_url)
|
||||
return True
|
||||
else:
|
||||
cli.log.error('%s exited %d', ' '.join(git_cmd), p.returncode)
|
||||
return False
|
||||
|
||||
|
||||
def git_clone_fork(fork, branch, force=False):
|
||||
if force:
|
||||
rmtree(cli.args.home)
|
||||
|
||||
git_url = '/'.join((cli.args.baseurl, fork))
|
||||
if git_clone(git_url, cli.args.home, branch):
|
||||
git_upstream(cli.args.home)
|
||||
else:
|
||||
exit(1)
|
||||
DEFAULT_BASE = 'https://github.com'
|
||||
DEFAULT_REPO = 'qmk_firmware'
|
||||
DEFAULT_FORK = 'qmk/' + DEFAULT_REPO
|
||||
DEFAULT_BRANCH = 'master'
|
||||
|
||||
|
||||
@cli.argument('-n', '--no', arg_only=True, action='store_true', help='Answer no to all questions')
|
||||
@cli.argument('-y', '--yes', arg_only=True, action='store_true', help='Answer yes to all questions')
|
||||
@cli.argument('--baseurl', arg_only=True, default=default_base, help='The URL all git operations start from. Default: %s' % default_base)
|
||||
@cli.argument('-b', '--branch', arg_only=True, default=default_branch, help='The branch to clone. Default: %s' % default_branch)
|
||||
@cli.argument('--baseurl', arg_only=True, default=DEFAULT_BASE, help='The URL all git operations start from. Default: %s' % DEFAULT_BASE)
|
||||
@cli.argument('-b', '--branch', arg_only=True, default=DEFAULT_BRANCH, help='The branch to clone. Default: %s' % DEFAULT_BRANCH)
|
||||
@cli.argument('-H', '--home', arg_only=True, default=Path(os.environ['QMK_HOME']), type=AbsPath, help='The location for QMK Firmware. Default: %s' % os.environ['QMK_HOME'])
|
||||
@cli.argument('fork', arg_only=True, default=default_fork, nargs='?', help='The qmk_firmware fork to clone. Default: %s' % default_fork)
|
||||
@cli.argument('fork', arg_only=True, default=DEFAULT_FORK, nargs='?', help='The qmk_firmware fork to clone. Default: %s' % DEFAULT_FORK)
|
||||
@cli.subcommand('Setup your computer for qmk_firmware.')
|
||||
def setup(cli):
|
||||
"""Guide the user through setting up their QMK environment.
|
||||
@@ -90,7 +52,7 @@ def setup(cli):
|
||||
if not yesno(delete_confirm, default=False):
|
||||
exit(1)
|
||||
|
||||
git_clone_fork(cli.args.fork, cli.args.branch, force=True)
|
||||
git_clone_fork(cli.args.home, cli.args.baseurl, cli.args.fork, cli.args.branch, force=True)
|
||||
|
||||
elif found_action == "Delete and clone a different fork":
|
||||
fork_name = question("Enter the name of the fork:", default=cli.args.fork)
|
||||
@@ -99,7 +61,7 @@ def setup(cli):
|
||||
if not yesno(delete_confirm, default=False):
|
||||
exit(1)
|
||||
|
||||
git_clone_fork(fork_name, branch_name, force=True)
|
||||
git_clone_fork(cli.args.home, cli.args.baseurl, fork_name, branch_name, force=True)
|
||||
|
||||
# Exists (but not an empty dir)
|
||||
elif cli.args.home.exists() and any(cli.args.home.iterdir()):
|
||||
@@ -114,7 +76,7 @@ def setup(cli):
|
||||
else:
|
||||
cli.log.error('Could not find qmk_firmware!')
|
||||
if yesno(clone_prompt):
|
||||
git_clone_fork(cli.args.fork, cli.args.branch)
|
||||
git_clone_fork(cli.args.home, cli.args.baseurl, cli.args.fork, cli.args.branch)
|
||||
else:
|
||||
cli.log.warning('Not cloning qmk_firmware due to user input or --no flag.')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user