Commit df05ebf3 authored by Davanum Srinivas's avatar Davanum Srinivas

Preserve order of egg_info section in setup.cfg

egg_info is the dictionary with information that is injected
into setup.cfg. edit_config uses RawConfigParser which uses
collections.OrderedDict for all the data. When we use a
simple dict(), when we loop through items in edit_config, we
see random behavior as a result the fields
tag_svn_revision/tag_date/tag_build are added to the setup.cfg
randomly. So if we sort the items by key when we traverse items
we will get deterministic output as RawConfigParser uses
OrderedDict internally by default.
parent fc6aec49
......@@ -2,6 +2,7 @@ from distutils.util import convert_path
from distutils import log
from distutils.errors import DistutilsOptionError
import distutils
import operator
import os
from setuptools.extern.six.moves import configparser
......@@ -42,7 +43,8 @@ def edit_config(filename, settings, dry_run=False):
log.debug("Reading configuration from %s", filename)
opts = configparser.RawConfigParser()
opts.read([filename])
for section, options in settings.items():
for section, options in sorted(settings.items(),
key=operator.itemgetter(0)):
if options is None:
log.info("Deleting section [%s] from %s", section, filename)
opts.remove_section(section)
......@@ -50,7 +52,8 @@ def edit_config(filename, settings, dry_run=False):
if not opts.has_section(section):
log.debug("Adding new section [%s] to %s", section, filename)
opts.add_section(section)
for option, value in options.items():
for option, value in sorted(options.items(),
key=operator.itemgetter(0)):
if value is None:
log.debug(
"Deleting %s.%s from %s",
......
import os
import glob
import re
import stat
import sys
try:
from unittest import mock
except ImportError:
import mock
from setuptools.command.egg_info import egg_info
from setuptools.dist import Distribution
from setuptools.extern.six.moves import map
import pytest
......@@ -59,6 +68,59 @@ class TestEggInfo(object):
})
yield env
def test_egg_info_save_version_info_setup_empty(self, tmpdir_cwd, env):
setup_cfg = os.path.join(env.paths['home'], 'setup.cfg')
build_files({
setup_cfg: DALS("""
[egg_info]
"""),
})
dist = Distribution()
ei = egg_info(dist)
ei.initialize_options()
ei.save_version_info(setup_cfg)
with open(setup_cfg, 'r') as f:
content = f.read()
assert '[egg_info]' in content
assert 'tag_build =' in content
assert 'tag_date = 0' in content
assert 'tag_svn_revision = 0' in content
if sys.version_info[0:2] >= (2, 7):
assert re.search('tag_build.*tag_date.*tag_svn_revision',
content,
re.MULTILINE | re.DOTALL) is not None
def test_egg_info_save_version_info_setup_defaults(self, tmpdir_cwd, env):
setup_cfg = os.path.join(env.paths['home'], 'setup.cfg')
build_files({
setup_cfg: DALS("""
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
"""),
})
dist = Distribution()
ei = egg_info(dist)
ei.initialize_options()
ei.save_version_info(setup_cfg)
with open(setup_cfg, 'r') as f:
content = f.read()
assert '[egg_info]' in content
assert 'tag_build =' in content
assert 'tag_date = 0' in content
assert 'tag_svn_revision = 0' in content
if sys.version_info[0:2] >= (2, 7):
assert re.search('tag_build.*tag_date.*tag_svn_revision',
content,
re.MULTILINE | re.DOTALL) is not None
def test_egg_base_installed_egg_info(self, tmpdir_cwd, env):
self._create_project()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment