Skip to content

Commit

Permalink
Merge pull request #29 from Anaconda-Platform/default-name
Browse files Browse the repository at this point in the history
Make the name field mandatory
  • Loading branch information
havocp committed Mar 7, 2017
2 parents 60dc348 + 6877869 commit 71014f4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 16 deletions.
3 changes: 1 addition & 2 deletions anaconda_project/commands/test/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def check(dirname):

out, err = capsys.readouterr()
assert (' added %s\n added %s\nCreated project archive %s\n' % (os.path.join(
os.path.basename(dirname), DEFAULT_PROJECT_FILENAME), os.path.join(
os.path.basename(dirname), "foo.py"), archivefile)) == out
"some_name", DEFAULT_PROJECT_FILENAME), os.path.join("some_name", "foo.py"), archivefile)) == out

with zipfile.ZipFile(archivefile, mode='r') as zf:
assert [os.path.basename(x) for x in sorted(zf.namelist())] == [DEFAULT_PROJECT_FILENAME, "foo.py"]
Expand Down
11 changes: 9 additions & 2 deletions anaconda_project/internal/test/tmpfile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ def complete_project_file_content(content):
yaml = _load_string(content)
if yaml is None:
raise AssertionError("Broken yaml: %r" % content)
elif 'env_specs' not in yaml:
modified = (content + "\n" + "env_specs:\n" + " default:\n" + " description: default\n" + "\n")

modified = content
if 'env_specs' not in yaml:
modified = (modified + "\n" + "env_specs:\n" + " default:\n" + " description: default\n" + "\n")

if 'name' not in yaml:
modified = (modified + "\n" + "name: some_name\n")

if modified is not content:
try:
# make sure we didn't mangle it
_load_string(modified)
Expand Down
19 changes: 18 additions & 1 deletion anaconda_project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ def update(self, project_file, conda_meta_file):
self.problem_strings = list([p.text for p in self.problems if not p.only_a_suggestion])

def _update_name(self, problems, project_file, conda_meta_file):
# For back-compat reasons, name=null means auto-name at runtime,
# while name field missing entirely is an error.
default_name = os.path.basename(self.directory_path)

if 'name' not in project_file.root:

def set_name_field(project):
project.project_file.set_value('name', default_name)

problems.append(ProjectProblem(text="The 'name:' field is missing.",
filename=project_file.filename,
fix_prompt=("Name the project '%s'?" % default_name),
fix_function=set_name_field))
# Note: we continue on here to set set the default name below,
# just to avoid dealing with `project.name is None` elsewhere
# in the code, but we don't save the name to the project_file.

name = project_file.get_value('name', None)
if name is not None:
if not is_string(name):
Expand All @@ -200,7 +217,7 @@ def _update_name(self, problems, project_file, conda_meta_file):
name = None

if name is None:
name = os.path.basename(self.directory_path)
name = default_name

self.name = name

Expand Down
8 changes: 6 additions & 2 deletions anaconda_project/project_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import os
from collections import OrderedDict
import json

from anaconda_project.yaml_file import YamlFile
from anaconda_project.env_spec import EnvSpec
Expand Down Expand Up @@ -143,8 +144,11 @@ def comment_out(comment):
# future: this is if/else is silly, we should be
# assigning these bodies up above when we assign the
# comments.
if section_name in ('name', 'icon'):
section_body = ""
if section_name == 'name':
default_name = os.path.basename(os.path.dirname(self.filename))
section_body = " " + json.dumps(default_name)
elif section_name in ('icon', ):
section_body = "" # empty body means null, not empty string
elif section_name in ('channels', 'packages'):
section_body = " []"
else:
Expand Down
2 changes: 2 additions & 0 deletions anaconda_project/test/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def check(dirname):
_pop_fake_env_creator()

with_directory_contents({DEFAULT_PROJECT_FILENAME: """
name: blah
env_specs:
foo: {}
bar: {}
Expand All @@ -366,6 +367,7 @@ def check(dirname):

with_directory_contents(
{DEFAULT_PROJECT_FILENAME: """
name: blah
env_specs:
default: {}
foo: {}
Expand Down
43 changes: 38 additions & 5 deletions anaconda_project/test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ def check_name_from_meta_file(dirname):
assert project.name == "foo"

with_directory_contents_completing_project_file(
{DEFAULT_RELATIVE_META_PATH: """
{DEFAULT_PROJECT_FILENAME: """
name: null
""",
DEFAULT_RELATIVE_META_PATH: """
package:
name: foo
"""}, check_name_from_meta_file)
Expand All @@ -310,10 +313,15 @@ def check_name_from_meta_file(dirname):
assert [(META_YAML_IN_ERRORS + ": package: name: field should have a string value not []")] == project.problems

with_directory_contents_completing_project_file(
{DEFAULT_RELATIVE_META_PATH: """
{
DEFAULT_PROJECT_FILENAME: """
name: null
""",
DEFAULT_RELATIVE_META_PATH: """
package:
name: []
"""}, check_name_from_meta_file)
"""
}, check_name_from_meta_file)


def test_get_name_from_project_file():
Expand Down Expand Up @@ -353,7 +361,12 @@ def check_name_from_directory_name(dirname):
project = project_no_dedicated_env(dirname)
assert project.name == os.path.basename(dirname)

with_directory_contents_completing_project_file(dict(), check_name_from_directory_name)
with_directory_contents_completing_project_file(
{
DEFAULT_PROJECT_FILENAME: """
name: null
"""
}, check_name_from_directory_name)


def test_set_name_in_project_file():
Expand Down Expand Up @@ -2395,6 +2408,25 @@ def fixit(project):
assert fixable.fix(None) == 42


def test_auto_fix_missing_name():
def check(dirname):
project = project_no_dedicated_env(dirname)
assert len(project.problems) == 1
assert len(project.problem_objects) == 1
problem = project.problem_objects[0]
assert problem.text == ("%s: The 'name:' field is missing." % DEFAULT_PROJECT_FILENAME)
assert problem.can_fix

problem.fix(project)
project.project_file.save()

assert project.problems == []
assert project.name == os.path.basename(dirname)
assert 'name' in project.project_file.root

with_directory_contents({DEFAULT_PROJECT_FILENAME: "env_specs:\n default: {}\n"}, check)


def test_auto_fix_missing_env_specs_section():
def check(dirname):
project = project_no_dedicated_env(dirname)
Expand Down Expand Up @@ -2785,6 +2817,7 @@ def check(dirname):
def test_empty_file_has_problems():
def check(dirname):
project = project_no_dedicated_env(dirname)
assert ['anaconda-project.yml: The env_specs section is empty.'] == project.problems
assert ["anaconda-project.yml: The 'name:' field is missing.",
"anaconda-project.yml: The env_specs section is empty."] == project.problems

with_directory_contents({DEFAULT_PROJECT_FILENAME: ""}, check)
11 changes: 7 additions & 4 deletions anaconda_project/test/test_project_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#
# Set the 'name' key to name your project
#
name:
name: <NAME>
#
# Set the 'icon' key to give your project an icon
#
Expand Down Expand Up @@ -103,7 +103,8 @@ def create_file(dirname):
assert os.path.exists(filename)
with codecs.open(filename, 'r', 'utf-8') as file:
contents = file.read()
assert expected_default_file == contents
expected = expected_default_file.replace("<NAME>", os.path.basename(dirname))
assert expected == contents

with_directory_contents(dict(), create_file)

Expand Down Expand Up @@ -200,7 +201,8 @@ def default_env_specs_func():
assert os.path.exists(filename)
with codecs.open(filename, 'r', 'utf-8') as file:
contents = file.read()
assert expected_one_env_spec_contents == contents
expected = expected_one_env_spec_contents.replace("<NAME>", os.path.basename(dirname))
assert expected == contents

with_directory_contents(dict(), create_file)

Expand Down Expand Up @@ -257,6 +259,7 @@ def default_env_specs_func():
assert os.path.exists(filename)
with codecs.open(filename, 'r', 'utf-8') as file:
contents = file.read()
assert expected_two_env_spec_contents == contents
expected = expected_two_env_spec_contents.replace("<NAME>", os.path.basename(dirname))
assert expected == contents

with_directory_contents(dict(), create_file)

0 comments on commit 71014f4

Please sign in to comment.