def test_release_major(clone_from_github_mock, check_output_mock, open_mock,
                       date_mock, expanduser_mock):
    repo_mock = Mock()
    repo_mock.latest_tag.return_value = '20.1.0'
    repo_mock.authors_since.return_value = ['*****@*****.**', '*****@*****.**']
    repo_mock.diff_name_status.return_value = [
        ('apis/foo/v1.ts', _git.Status.ADDED),
        ('apis/bar/v1.ts', _git.Status.DELETED),
        ('apis/baz/v1.ts', _git.Status.UPDATED),
    ]
    side_effect = common.clone_from_github_mock_side_effect(repo_mock)
    clone_from_github_mock.side_effect = side_effect
    check_output_mock.return_value = '20.1.0'
    open_package_json_mock = mock_open(read_data=('{"version": "20.1.0"}'))
    open_changelog_md_mock = mock_open(read_data='...\n')
    open_npmrc_mock = mock_open()
    open_index_md_mock = mock_open(read_data=('...\n\n'
                                              '### ...\n\n'
                                              '* [v20.1.0 (latest)](...)\n'
                                              '...\n'))
    open_mock.side_effect = [
        open_package_json_mock.return_value,
        open_package_json_mock.return_value,
        open_changelog_md_mock.return_value,
        open_changelog_md_mock.return_value, open_npmrc_mock.return_value,
        open_index_md_mock.return_value, open_index_md_mock.return_value
    ]
    date_mock.today.return_value.strftime.return_value = '1 September 2017'
    expanduser_mock.side_effect = lambda x: '/home/test' + x[1:]

    manager = Mock()
    manager.attach_mock(clone_from_github_mock, 'clone_from_github')
    manager.attach_mock(check_output_mock, 'check_output')
    manager.attach_mock(open_mock, 'open')
    manager.attach_mock(repo_mock, 'repo')
    manager.attach_mock(open_package_json_mock, 'open_package_json')
    manager.attach_mock(open_changelog_md_mock, 'open_changelog_md')
    manager.attach_mock(open_npmrc_mock, 'open_npmrc')
    manager.attach_mock(open_index_md_mock, 'open_index_md')

    google_api_nodejs_client.release('/tmp', common.GITHUB_ACCOUNT,
                                     _NPM_ACCOUNT)
    assert manager.mock_calls == [
        call.clone_from_github('google/google-api-nodejs-client',
                               '/tmp/google-api-nodejs-client',
                               github_account=common.GITHUB_ACCOUNT),
        call.repo.latest_tag(),
        call.repo.authors_since('20.1.0'),
        call.check_output(['npm', 'view', 'googleapis', 'version']),
        call.repo.diff_name_status(rev='20.1.0'),
        call.open('/tmp/google-api-nodejs-client/package.json'),
        call.open_package_json().__enter__(),
        call.open_package_json().read(),
        call.open_package_json().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/package.json', 'w'),
        call.open_package_json().__enter__(),
        call.open_package_json().write('{\n  "version": "21.0.0"\n}'),
        call.open_package_json().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/CHANGELOG.md'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().read(),
        call.open_changelog_md().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/CHANGELOG.md', 'w'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().write(('##### 21.0.0 - 1 September 2017\n\n'
                                        '###### Breaking changes\n'
                                        '- Deleted `bar`\n\n'
                                        '###### Backwards compatible changes\n'
                                        '- Added `foo`\n'
                                        '- Updated `baz`\n\n'
                                        '...\n')),
        call.open_changelog_md().__exit__(None, None, None),
        call.check_output(['npm', 'install'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['make', 'build'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['make', 'test'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.repo.commit('21.0.0', 'Alice', '*****@*****.**'),
        call.repo.tag('21.0.0'),
        call.repo.push(),
        call.repo.push(tags=True),
        call.open('/home/test/.npmrc', 'w'),
        call.open_npmrc().__enter__(),
        call.open_npmrc().write(
            '//registry.npmjs.org/:_authToken=auth_token\n'),
        call.open_npmrc().__exit__(None, None, None),
        call.check_output(['npm', 'publish'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['make', 'docs'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.repo.checkout('gh-pages'),
        call.check_output(['rm', '-rf', 'latest'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['cp', '-r', 'doc/googleapis/21.0.0', 'latest'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['cp', '-r', 'doc/googleapis/21.0.0', '21.0.0'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.open('/tmp/google-api-nodejs-client/index.md'),
        call.open_index_md().__enter__(),
        call.open_index_md().readlines(),
        call.open_index_md().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/index.md', 'w'),
        call.open_index_md().__enter__(),
        call.open_index_md().write(
            ('...\n\n'
             '### ...\n\n'
             '* [v21.0.0 (latest)]'
             '(http://google.github.io/google-api-nodejs-client/21.0.0'
             '/index.html)\n'
             '* [v20.1.0](...)\n'
             '...\n')),
        call.open_index_md().__exit__(None, None, None),
        call.repo.add(['latest', '21.0.0']),
        call.repo.commit('21.0.0', 'Alice', '*****@*****.**'),
        call.repo.push(branch='gh-pages'),
        call.repo.checkout('master')
    ]
def test_release_invalid_index_md(clone_from_github_mock,
                                  check_output_mock,
                                  open_mock,
                                  date_mock,
                                  expanduser_mock):
    repo_mock = Mock()
    repo_mock.latest_tag.return_value = '20.1.0'
    repo_mock.authors_since.return_value = ['*****@*****.**', '*****@*****.**']
    repo_mock.diff_name_status.return_value = [
        ('apis/foo/v1.ts', _git.Status.ADDED),
        ('apis/baz/v1.ts', _git.Status.UPDATED),
    ]
    side_effect = common.clone_from_github_mock_side_effect(repo_mock)
    clone_from_github_mock.side_effect = side_effect
    check_output_mock.return_value = '20.1.0'
    open_package_json_mock = mock_open(read_data=('{"version": "20.1.0"}'))
    open_changelog_md_mock = mock_open(read_data='...\n')
    open_npmrc_mock = mock_open()
    open_index_md_mock = mock_open(read_data=('...\n'))
    open_mock.side_effect = [
        open_package_json_mock.return_value,
        open_package_json_mock.return_value,
        open_changelog_md_mock.return_value,
        open_changelog_md_mock.return_value,
        open_npmrc_mock.return_value,
        open_index_md_mock.return_value,
    ]
    date_mock.today.return_value.strftime.return_value = '1 September 2017'
    expanduser_mock.side_effect = lambda x: '/home/test' + x[1:]

    manager = Mock()
    manager.attach_mock(clone_from_github_mock, 'clone_from_github')
    manager.attach_mock(check_output_mock, 'check_output')
    manager.attach_mock(open_mock, 'open')
    manager.attach_mock(repo_mock, 'repo')
    manager.attach_mock(open_package_json_mock, 'open_package_json')
    manager.attach_mock(open_changelog_md_mock, 'open_changelog_md')
    manager.attach_mock(open_npmrc_mock, 'open_npmrc')
    manager.attach_mock(open_index_md_mock, 'open_index_md')

    with pytest.raises(Exception) as excinfo:
        google_api_nodejs_client.release(
            '/tmp', common.GITHUB_ACCOUNT, _NPM_ACCOUNT)
    assert str(excinfo.value) == 'index.md has an unexpected format'
    assert manager.mock_calls == [
        call.clone_from_github('google/google-api-nodejs-client',
                               '/tmp/google-api-nodejs-client',
                               github_account=common.GITHUB_ACCOUNT),
        call.repo.latest_tag(),
        call.repo.authors_since('20.1.0'),
        call.check_output(['npm', 'view', 'googleapis', 'version']),
        call.repo.diff_name_status(rev='20.1.0'),
        call.open('/tmp/google-api-nodejs-client/package.json'),
        call.open_package_json().__enter__(),
        call.open_package_json().read(),
        call.open_package_json().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/package.json', 'w'),
        call.open_package_json().__enter__(),
        call.open_package_json().write('{\n  "version": "20.2.0"\n}'),
        call.open_package_json().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/CHANGELOG.md'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().read(),
        call.open_changelog_md().__exit__(None, None, None),
        call.open('/tmp/google-api-nodejs-client/CHANGELOG.md', 'w'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().write(('##### 20.2.0 - 1 September 2017\n\n'
                                        '###### Backwards compatible changes\n'
                                        '- Added `foo`\n'
                                        '- Updated `baz`\n\n'
                                        '...\n')),
        call.open_changelog_md().__exit__(None, None, None),
        call.check_output(['npm', 'install'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['node', '--max_old_space_size=2000', '/usr/bin/npm',
                           'run', 'build'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['npm', 'run', 'test'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.repo.commit('20.2.0', 'Alice', '*****@*****.**'),
        call.repo.tag('20.2.0'),
        call.repo.push(),
        call.repo.push(tags=True),
        call.open('/home/test/.npmrc', 'w'),
        call.open_npmrc().__enter__(),
        call.open_npmrc().write(
            '//registry.npmjs.org/:_authToken=auth_token\n'),
        call.open_npmrc().__exit__(None, None, None),
        call.check_output(['npm', 'publish'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['npm', 'run', 'doc'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.repo.checkout('gh-pages'),
        call.check_output(['rm', '-rf', 'latest'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['cp', '-r', 'doc/googleapis/20.2.0', 'latest'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.check_output(['cp', '-r', 'doc/googleapis/20.2.0', '20.2.0'],
                          cwd='/tmp/google-api-nodejs-client'),
        call.open('/tmp/google-api-nodejs-client/index.md'),
        call.open_index_md().__enter__(),
        call.open_index_md().readlines(),
        call.open_index_md().__exit__(None, None, None),
    ]
def test_release_patch(clone_from_github_mock, check_output_mock, open_mock,
                       expanduser_mock, chmod_mock):
    repo_mock = Mock()
    repo_mock.latest_tag.return_value = '0.13.6'
    repo_mock.authors_since.return_value = ['*****@*****.**', '*****@*****.**']
    repo_mock.diff_name_status.return_value = [
        ('generated/google/apis/foo_v1.rb', _git.Status.ADDED),
        ('generated/google/apis/baz_v1.rb', _git.Status.UPDATED),
    ]
    side_effect = common.clone_from_github_mock_side_effect(repo_mock)
    clone_from_github_mock.side_effect = side_effect
    check_output_mock.return_value = 'google-api-client (0.13.6)'
    open_version_rb_mock = mock_open(
        read_data=('...\n'
                   'module Google\n'
                   '    module Apis\n'
                   '        # Client library version\n'
                   '        VERSION = \'0.13.6\'\n'
                   '    ...\n'))
    open_changelog_md_mock = mock_open(read_data='...\n')
    open_credentials_mock = mock_open()
    open_mock.side_effect = [
        open_version_rb_mock.return_value, open_version_rb_mock.return_value,
        open_changelog_md_mock.return_value,
        open_changelog_md_mock.return_value, open_credentials_mock.return_value
    ]
    expanduser_mock.side_effect = lambda x: '/home/test' + x[1:]

    manager = Mock()
    manager.attach_mock(clone_from_github_mock, 'clone_from_github')
    manager.attach_mock(check_output_mock, 'check_output')
    manager.attach_mock(open_mock, 'open')
    manager.attach_mock(repo_mock, 'repo')
    manager.attach_mock(chmod_mock, 'chmod')
    manager.attach_mock(open_version_rb_mock, 'open_version_rb')
    manager.attach_mock(open_changelog_md_mock, 'open_changelog_md')
    manager.attach_mock(open_credentials_mock, 'open_credentials')

    google_api_ruby_client.release('/tmp', common.GITHUB_ACCOUNT,
                                   _RUBYGEMS_ACCOUNT)
    assert manager.mock_calls == [
        call.clone_from_github('google/google-api-ruby-client',
                               '/tmp/google-api-ruby-client',
                               github_account=common.GITHUB_ACCOUNT),
        call.repo.latest_tag(),
        call.repo.authors_since('0.13.6'),
        call.check_output(['gem', 'search', '-r', '^google-api-client$']),
        call.repo.diff_name_status(rev='0.13.6'),
        call.open('/tmp/google-api-ruby-client/lib/google/apis/version.rb'),
        call.open_version_rb().__enter__(),
        call.open_version_rb().read(),
        call.open_version_rb().__exit__(None, None, None),
        call.open('/tmp/google-api-ruby-client/lib/google/apis/version.rb',
                  'w'),
        call.open_version_rb().__enter__(),
        call.open_version_rb().write(('...\n'
                                      'module Google\n'
                                      '    module Apis\n'
                                      '        # Client library version\n'
                                      '        VERSION = \'0.13.7\'\n'
                                      '    ...\n')),
        call.open_version_rb().__exit__(None, None, None),
        call.open('/tmp/google-api-ruby-client/CHANGELOG.md'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().read(),
        call.open_changelog_md().__exit__(None, None, None),
        call.open('/tmp/google-api-ruby-client/CHANGELOG.md', 'w'),
        call.open_changelog_md().__enter__(),
        call.open_changelog_md().write(('# 0.13.7\n'
                                        '* Backwards compatible changes:\n'
                                        '  * Added `foo_v1`\n'
                                        '  * Updated `baz_v1`\n\n'
                                        '...\n')),
        call.open_changelog_md().__exit__(None, None, None),
        call.check_output(['bundle', 'install', '--path', 'vendor/bundle'],
                          cwd='/tmp/google-api-ruby-client'),
        call.check_output(['bundle', 'exec', 'rake', 'spec'],
                          cwd='/tmp/google-api-ruby-client'),
        call.repo.commit('0.13.7', 'Alice', '*****@*****.**'),
        call.repo.tag('0.13.7'),
        call.repo.push(),
        call.repo.push(tags=True),
        call.check_output(['./script/package'],
                          cwd='/tmp/google-api-ruby-client'),
        call.open('/home/test/.gem/credentials', 'w'),
        call.open_credentials().__enter__(),
        call.open_credentials().write('---\n:rubygems_api_key: api_key\n'),
        call.open_credentials().__exit__(None, None, None),
        call.chmod('/home/test/.gem/credentials', 0o600),
        call.check_output(['gem', 'push', 'pkg/google-api-client-0.13.7.gem'],
                          cwd='/tmp/google-api-ruby-client')
    ]