Skip to content

Commit

Permalink
Feature/profile force new (#4880)
Browse files Browse the repository at this point in the history
* Add a test to check behaviour of the force option of the profile new

* Add a force flag to profile new to not raise an error when the profile exists
  • Loading branch information
Minimonium authored and memsharded committed Mar 31, 2019
1 parent 7d2d5e5 commit 4d53b29
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions conans/client/cmd/profile.py
Expand Up @@ -35,10 +35,10 @@ def cmd_profile_list(cache_profiles_path, output):
return profiles


def cmd_profile_create(profile_name, cache_profiles_path, output, detect=False):
def cmd_profile_create(profile_name, cache_profiles_path, output, detect=False, force=False):
profile_path = get_profile_path(profile_name, cache_profiles_path, get_cwd(),
exists=False)
if os.path.exists(profile_path):
if not force and os.path.exists(profile_path):
raise ConanException("Profile already exists")

profile = Profile()
Expand Down
4 changes: 3 additions & 1 deletion conans/client/command.py
Expand Up @@ -1318,6 +1318,8 @@ def profile(self, *args):
"folder or path and name for a profile file")
parser_new.add_argument("--detect", action='store_true', default=False,
help='Autodetect settings and fill [settings] section')
parser_new.add_argument("--force", action='store_true', default=False,
help='Overwrite existing profile if existing')

parser_update = subparsers.add_parser('update', help='Update a profile with desired value')
parser_update.add_argument('item',
Expand Down Expand Up @@ -1346,7 +1348,7 @@ def profile(self, *args):
profile_text = self._conan.read_profile(profile)
self._outputer.print_profile(profile, profile_text)
elif args.subcommand == "new":
self._conan.create_profile(profile, args.detect)
self._conan.create_profile(profile, args.detect, args.force)
elif args.subcommand == "update":
try:
key, value = args.item.split("=", 1)
Expand Down
4 changes: 2 additions & 2 deletions conans/client/conan_api.py
Expand Up @@ -945,9 +945,9 @@ def profile_list(self):
return cmd_profile_list(self._cache.profiles_path, self._user_io.out)

@api_method
def create_profile(self, profile_name, detect=False):
def create_profile(self, profile_name, detect=False, force=False):
return cmd_profile_create(profile_name, self._cache.profiles_path,
self._user_io.out, detect)
self._user_io.out, detect, force)

@api_method
def update_profile(self, profile_name, key, value):
Expand Down
32 changes: 32 additions & 0 deletions conans/test/functional/command/profile_test.py
Expand Up @@ -184,3 +184,35 @@ def profile_new_test(self):
pr_path = os.path.join(client.cache.profiles_path, "MyProfile3")
self.assertTrue(os.path.exists(pr_path))
self.assertNotIn("os=", load(pr_path))

def profile_force_new_test(self):
client = TestClient()

empty_profile = """[settings]
[options]
[build_requires]
[env]
"""
client.run("profile new ./MyProfile")
pr_path = os.path.join(client.current_folder, "MyProfile")
self.assertTrue(os.path.exists(pr_path))
self.assertEqual(load(pr_path), empty_profile)

client.run("profile new ./MyProfile --detect", assert_error=True)
self.assertIn("Profile already exists", client.user_io.out)

client.run("profile new ./MyProfile --detect --force", assert_error=False)
self.assertNotEqual(load(pr_path), empty_profile)

detected_profile = load(pr_path)

client.run("profile update settings.os=FakeOS ./MyProfile")
self.assertIn("\nos=FakeOS", load(pr_path))

client.run("profile update env.MyEnv=MYVALUe ./MyProfile")
self.assertIn("[env]\nMyEnv=MYVALUe", load(pr_path))

client.run("profile new ./MyProfile --detect --force", assert_error=False)
self.assertNotIn("\nos=FakeOS", load(pr_path))
self.assertNotIn("[env]\nMyEnv=MYVALUe", load(pr_path))
self.assertEqual(load(pr_path), detected_profile)

0 comments on commit 4d53b29

Please sign in to comment.