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_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')
    ]