Skip to content

Commit 07496ef

Browse files
authored
fix(core): test and fix Profile.from_config_file_and_env (#54)
1 parent 2548733 commit 07496ef

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

scaleway-core/scaleway_core/profile/profile.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,15 @@ class ProfileConfig:
8585

8686
@dataclass
8787
class Profile(ProfileDefaults, ProfileConfig):
88-
@classmethod
89-
def merge(cls: Type[ProfileSelf], other: ProfileSelf) -> ProfileSelf:
88+
def merge(self, other: Profile) -> None:
9089
"""
91-
Create a new profile by merging the current profile with another one.
90+
Merge the current profile with another one.
9291
"""
93-
fields = dataclasses.fields(Profile)
94-
merged = {}
95-
96-
for field in fields:
97-
merged[field.name] = getattr(cls, field.name) or getattr(other, field.name)
92+
for field in dataclasses.fields(Profile):
93+
current_value = getattr(self, field.name)
9894

99-
return cls(**merged)
95+
if current_value is None:
96+
setattr(self, field.name, getattr(other, field.name))
10097

10198
@classmethod
10299
def from_env(cls: Type[ProfileSelf]) -> ProfileSelf:
@@ -192,7 +189,8 @@ def from_config_file_and_env(
192189

193190
try:
194191
a = cls.from_config_file(filepath, profile_name)
195-
return profile.merge(a)
192+
profile.merge(a)
196193
except Exception as e:
197194
print(e)
198-
return profile
195+
196+
return profile

scaleway-core/tests/test_profile_env.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from unittest import mock
77

88
import utils
9-
109
from scaleway_core.profile import Profile
1110

1211
logger = logging.getLogger()

scaleway-core/tests/test_profile_file.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import tempfile
55
import unittest
66
import uuid
7+
from unittest import mock
78

89
import utils
9-
1010
from scaleway_core.profile import Profile
1111

1212
logger = logging.getLogger()
@@ -65,3 +65,13 @@ def test_load_profile_from_config_file(self) -> None:
6565
def test_load_profile_from_config_file_with_profile(self) -> None:
6666
profile = Profile.from_config_file(self.profile_file_name, "demo")
6767
self.assertEqual(profile, self.demo_profile_config)
68+
69+
def test_load_profile_from_config_file_and_env(self) -> None:
70+
with mock.patch.dict(
71+
os.environ,
72+
{
73+
"SCW_SECRET_KEY": "11111111-1111-1111-1111-111111111111",
74+
},
75+
):
76+
profile = Profile.from_config_file_and_env(filepath=self.profile_file_name)
77+
self.assertEqual(profile.secret_key, "11111111-1111-1111-1111-111111111111")

0 commit comments

Comments
 (0)