def test_commit(wst): with temp_dir(): with pytest.raises(SystemExit): wst('commit') config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): test_cleanrun with pytest.raises(SystemExit): wst('commit "no files to commit"') with temp_git_repo(): run('git checkout -b 3.0.x') with open('new_file_commit1', 'w') as fp: fp.write('New World') run('git add -A') wst('commit "Add new file"') changes = run('git log --oneline', return_output=True) expected_commit_message = "Add new file" assert expected_commit_message in changes # test commit with branch with open('new_file_commit2', 'w') as fp: fp.write('Hello World') wst('commit "New Master file" --branch master') changes = run('git log --oneline', return_output=True) expected_commit_message = "New Master file" assert expected_commit_message in changes
def test_merge_downstream(wst, capsys): config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): run('git commit --allow-empty -m dummy-commit') run('git branch 3.0.x') run('git checkout -b 2.0.x') run('git commit --allow-empty -m new-commit') wst('merge --downstream') branches = run('git branch', return_output=True) changes = run('git log --oneline', return_output=True) out, _ = capsys.readouterr() assert out == """\ Merging 2.0.x into 3.0.x Pushing 3.0.x Merging 3.0.x into master Pushing master """ assert '* master' in branches assert 'new-commit' in changes
def test_merge_branch_skip_all(wst, capsys): """ Test to check if merge_branch works with three commits created on 3.0.x, with the all commits skipped. """ config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): # Dummy commit run('git commit --allow-empty -m dummy-commit') run('git checkout -b 3.0.x') skipped1_sha = make_commit("commit1", skip=True) skipped2_sha = make_commit("commit2", skip=True) skipped3_sha = make_commit("commit3", skip=True) run('git checkout master') wst('merge 3.0.x --skip-commits \'skip\' \'space separated example\'') changes = run('git log --oneline', return_output=True) # Change should have been in the log entry assert f'Merge commit {skipped1_sha[0:7]} into master (using strategy ours)' in changes assert f'Merge commit {skipped2_sha[0:7]} into master (using strategy ours)' in changes assert f'Merge commit {skipped3_sha[0:7]} into master (using strategy ours)' in changes assert '[skip]' in changes assert '[skip]' in changes assert '[skip]' in changes temp_git_dir_path = os.getcwd() assert not os.path.isfile( os.path.join(temp_git_dir_path, "commit1.xml")) assert not os.path.isfile( os.path.join(temp_git_dir_path, "commit2.xml")) assert not os.path.isfile( os.path.join(temp_git_dir_path, "commit3.xml")) out, _ = capsys.readouterr() assert out.split('\n')[0] == 'Merging 3.0.x into master'
def test_merge_branch_skip_none(wst, capsys): """ Test to check if merge_branch works when three commits are created, with no commits skipped. Fourth commit used so that all 3 commits on 3.0.x create merge commits. """ config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): # Dummy commit run('git commit --allow-empty -m dummy-commit') run('git checkout -b 3.0.x') commit1_sha = make_commit("commit1") commit2_sha = make_commit("commit2") commit3_sha = make_commit("commit3") run('git checkout master') make_commit("commit4") wst('merge 3.0.x --skip-commits \'skip\' \'space separated example\'') changes = run('git log --oneline', return_output=True) assert f'Merge commit \'{commit1_sha[0:7]}\'' in changes assert f'Merge commit \'{commit2_sha[0:7]}\'' in changes assert f'Merge commit \'{commit3_sha[0:7]}\'' in changes assert 'commit1' in changes assert 'commit2' in changes assert 'commit3' in changes temp_git_dir_path = os.getcwd() assert os.path.isfile(os.path.join(temp_git_dir_path, "commit1.xml")) assert os.path.isfile(os.path.join(temp_git_dir_path, "commit2.xml")) assert os.path.isfile(os.path.join(temp_git_dir_path, "commit3.xml")) out, _ = capsys.readouterr() assert out.split('\n')[0] == 'Merging 3.0.x into master'
def test_merge_branch_with_whitelist(wst, capsys): config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): # Dummy commit run('git commit --allow-empty -m dummy-commit') run('git checkout -b 3.0.x') run('touch temp.xml') run('git add -A') # Commit 1 run('git commit -m [skip]') # commit 2 run('touch temp2.xml') run('git add -A') run('git commit -m commit2') run('git checkout master') wst('merge 3.0.x --skip-commits \'skip\' \'space separated example\'') changes = run('git log --oneline', return_output=True) # Change should have been in the log entry assert 'Merge branch \'3.0.x\'' in changes assert '[skip]' in changes assert 'commit2' in changes out, _ = capsys.readouterr() assert out.split('\n')[0] == 'Merging 3.0.x into master'
def test_test(wst, monkeypatch): if 'PYTESTARGS' in os.environ: del os.environ['PYTESTARGS'] with temp_dir() as cwd: monkeypatch.setenv('HOME', cwd) # tox creates virtualenvs in ~/.virtualenvs with pytest.raises(SystemExit): wst('test') with temp_git_repo(name='foo') as cwd: with pytest.raises(SystemExit): wst('test') wst('setup --product') with open('foo/__init__.py', 'w') as fp: fp.write('hello = "world"') pass_test = 'from foo import hello\n\n\ndef test_pass():\n assert hello == "world"' fail_test = 'def test_fail():\n assert False' with open('tests/test_pass.py', 'w') as fp: fp.write(pass_test) commands = wst('test') assert set(commands.keys()) == {'cover', 'py37', 'style'} assert 'tox' in commands['cover'] wst('test --show-dependencies') wst('test --install-editable flake8') wst('test --install-editable foo') results = wst('test --test-dependents') assert set(results.keys()) == {'foo'} assert '2 passed' in results['foo'] with open('tests/test_fail.py', 'w') as fp: fp.write(pass_test + '\n\n\n' + fail_test) with pytest.raises(SystemExit): wst('test') output = wst('test tests/test_pass.py') assert output == {'py36': 'pytest {env:PYTESTARGS:}', 'py37': 'pytest {env:PYTESTARGS:}'} os.utime('requirements.txt', None) assert list(wst('test -k test_pass').keys()) == ['py36', 'py37'] with open('tests/test_fail.py', 'w') as fp: fp.write(pass_test + '\n' + fail_test) with pytest.raises(SystemExit): wst('test style') with open('tests/test_fail.py', 'w') as fp: fp.write(pass_test + '\n\n\n' + fail_test) assert 'style' in wst('test style') os.unlink('tests/test_fail.py') assert 'cover' in wst('test cover') assert os.path.exists('coverage.xml') assert os.path.exists('htmlcov/index.html')
def test_sanity(wst, command, exception): with temp_dir(): if exception: with pytest.raises(exception): wst(command) else: wst(command) with temp_git_repo(): wst(command)
def test_merge_branch(wst, capsys): config.merge.branches = '1.0.x 2.0.x 3.0.x master' with temp_git_repo(): run('git commit --allow-empty -m dummy-commit') run('git checkout -b 3.0.x') run('git commit --allow-empty -m new-commit') run('git checkout master') wst('merge 3.0.x') changes = run('git log --oneline', return_output=True) assert 'new-commit' in changes out, _ = capsys.readouterr() assert out.split('\n')[0] == 'Merging 3.0.x into master'
def test_setup(wst, monkeypatch): with temp_git_repo(name='foo') as tmpdir: bashrc_file = os.path.join(tmpdir, '.bashrc') wstrc_file = os.path.join(tmpdir, '.wstrc') with open(bashrc_file, 'w') as fp: fp.write('export EXISTING=true') monkeypatch.setattr('workspace.commands.setup.BASHRC_FILE', os.path.join(tmpdir, '.bashrc')) monkeypatch.setattr('workspace.commands.setup.WSTRC_FILE', os.path.join(tmpdir, '.wstrc')) wst('setup --commands-with-aliases') bashrc = open(bashrc_file).read().split('\n') wstrc = open(wstrc_file).read() assert bashrc[0] == 'export EXISTING=true' assert bashrc[1] == '' assert bashrc[2].startswith('source ') and bashrc[2].endswith('.wstrc') assert 'function ws()' in wstrc
def test_commit(wst): with temp_dir(): with pytest.raises(SystemExit): wst('commit') with temp_git_repo(): with pytest.raises(SystemExit): wst('commit "no files to commit"') with open('new_file', 'w') as fp: fp.write('Hello World') assert 'new_file' in stat_repo(return_output=True) wst('commit "Add new file" --branch master') assert 'working tree clean' in stat_repo(return_output=True) assert 'Hello World' == open('new_file').read() with open('new_file', 'w') as fp: fp.write('New World') wst('commit "Update file"') assert ['update-file@master', 'master'] == all_branches() wst('commit --move release') assert ['update-file@master', 'master', 'release'] == all_branches() wst('commit --discard') assert ['master', 'release'] == all_branches() wst('checkout release') wst('commit --discard') assert ['release', 'master'] == all_branches() logs = commit_logs() assert 'new file' in logs assert 1 == len(list(filter(None, logs.split('commit'))))
def test_status(wst, capsys): with temp_git_repo(): wst('status') out, _ = capsys.readouterr() assert out == '# Branches: \n' run('git commit --allow-empty -m Dummy') wst('status') out, _ = capsys.readouterr() assert out == '# Branches: master\n' run('git checkout -b feature') wst('status') out, _ = capsys.readouterr() assert out == '# Branches: feature master\n' run('git checkout HEAD^0') wst('status') out, _ = capsys.readouterr() assert re.fullmatch('# Branches: \w+\* feature master\n', out)
def test_cleanrun(wst): config.clean.remove_products_older_than_days = 30 with temp_dir(): repos = ['repo', 'old_repo', 'old_repo_dirty'] run('touch file; mkdir ' + ' '.join(repos), shell=True) for repo in repos: run('cd {}; git init; git commit --allow-empty -m "Initial commit"'.format(repo), shell=True) if repo.startswith('old'): run('touch -t 200001181205.09 ' + repo) run('cd old_repo_dirty; touch new_file', shell=True) run('ls -l') wst('clean') assert os.listdir() == ['old_repo_dirty', 'repo', 'file'] with temp_git_repo(): run('touch hello.py hello.pyc') wst('clean') assert os.listdir() == ['.git', 'hello.py']
def test_publish(wst, monkeypatch): silent_run_mock = Mock() config_mock = Mock() monkeypatch.setattr('workspace.commands.publish.LocalConfig', config_mock) monkeypatch.setattr('workspace.commands.publish.silent_run', silent_run_mock) monkeypatch.setattr('workspace.commands.publish.run', silent_run_mock) config_mock().get.side_effect = ['repo', 'user', 'pass'] * 10 with temp_git_repo() as cwd: wst('setup --product') # Patch release run('git commit --allow-empty -m change1') run('git commit --allow-empty -m change2') wst('publish') changes = open('docs/CHANGELOG.rst').read() assert changes == """\ Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='0.0.2'," python = Path('~/.virtualenvs').expanduser() / Path( cwd).name / 'bin' / 'python' repo_path = '/private' + str(cwd) assert silent_run_mock.call_args_list == [ call('rm -rf dist/*', cwd=repo_path, shell=True), call(f'{python} setup.py sdist bdist_wheel', cwd=repo_path), call('twine upload -r "pypi" -u "user" -p "pass" dist/*', cwd=repo_path, shell=True, silent=2) ] # No changes with pytest.raises(SystemExit): wst('publish') # Minor release run('git commit --allow-empty -m feature1') wst('publish --minor') assert 'Bump minor version' in commit_logs() changes = open('docs/CHANGELOG.rst').read() print(changes) assert changes == """\ Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='0.1.1'," # Major release run('git commit --allow-empty -m feature2') wst('publish --major') assert 'Bump major version' in commit_logs() changes = open('docs/CHANGELOG.rst').read() print(changes) assert changes == """\ Version 1.0.0 ================================================================================ * feature2 Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='1.0.1'," # Already published patch version will bump before publish run('git commit --allow-empty -m "Publish version 1.0.1"', shell=True) run('git commit --allow-empty -m bugfix1') wst('publish') changes = open('docs/CHANGELOG.rst').read() assert changes == """\ Version 1.0.2 ================================================================================ * bugfix1 Version 1.0.0 -------------------------------------------------------------------------------- * feature2 Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='1.0.3',"
def test_publish(wst, monkeypatch): silent_run_mock = Mock() config_mock = Mock() monkeypatch.setattr('workspace.commands.publish.LocalConfig', config_mock) monkeypatch.setattr('workspace.commands.publish.silent_run', silent_run_mock) monkeypatch.setattr('workspace.commands.publish.run', silent_run_mock) config_mock().get.side_effect = ['repo', 'user', 'pass'] * 10 with temp_git_repo() as cwd: wst('setup --product') # Patch release run('git commit --allow-empty -m change1') run('git commit --allow-empty -m change2') wst('publish') changes = open('docs/CHANGELOG.rst').read() assert changes == """\ Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='0.0.2'," python = Path('~/.virtualenvs').expanduser() / Path(cwd).name / 'bin' / 'python' assert silent_run_mock.call_args_list == [ call('rm -rf dist/*', cwd=str(cwd), shell=True), call(f'{python} setup.py sdist bdist_wheel', cwd=str(cwd)), call('twine upload -r "pypi" -u "user" -p "pass" dist/*', cwd=str(cwd), shell=True, silent=2)] # No changes with pytest.raises(SystemExit): wst('publish') # Minor release run('git commit --allow-empty -m feature1') wst('publish --minor') assert 'Bump minor version' in commit_logs() changes = open('docs/CHANGELOG.rst').read() print(changes) assert changes == """\ Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='0.1.1'," # Major release run('git commit --allow-empty -m feature2') wst('publish --major') assert 'Bump major version' in commit_logs() changes = open('docs/CHANGELOG.rst').read() print(changes) assert changes == """\ Version 1.0.0 ================================================================================ * feature2 Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='1.0.1'," # Already published patch version will bump before publish run('git commit --allow-empty -m "Publish version 1.0.1"', shell=True) run('git commit --allow-empty -m bugfix1') wst('publish') changes = open('docs/CHANGELOG.rst').read() assert changes == """\ Version 1.0.2 ================================================================================ * bugfix1 Version 1.0.0 -------------------------------------------------------------------------------- * feature2 Version 0.1.0 ================================================================================ * feature1 Version 0.0.1 ================================================================================ * change2 * change1 """ setup_py = open('setup.py').read().split('\n') assert setup_py[5] == " version='1.0.3',"