Skip to content

Commit

Permalink
fix tests and empty value
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Sorokin <dmd40in@gmail.com>
  • Loading branch information
DimedS committed Mar 5, 2024
1 parent 2735206 commit a17da4a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
54 changes: 29 additions & 25 deletions kedro-telemetry/kedro_telemetry/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,35 @@ def _get_or_create_uuid():
"""
Reads a UUID from a configuration file or generates and saves a new one if not present.
"""

config_path = user_config_dir("kedro")
full_path = os.path.join(config_path, CONFIG_FILENAME)
config = ConfigParser()

if os.path.exists(full_path):
config.read(full_path)

if config.has_section("telemetry") and "uuid" in config["telemetry"]:
try:
return uuid.UUID(config["telemetry"]["uuid"]).hex
except ValueError:
pass # Invalid UUID found, will generate a new one

# Generate a new UUID and save it to the config file
if not config.has_section("telemetry"):
config.add_section("telemetry")
new_uuid = uuid.uuid4().hex
config.set("telemetry", "uuid", new_uuid)

os.makedirs(config_path, exist_ok=True)
with open(full_path, "w") as configfile:
config.write(configfile)

return new_uuid
try:
config_path = user_config_dir("kedro")
full_path = os.path.join(config_path, CONFIG_FILENAME)
config = ConfigParser()

if os.path.exists(full_path):
config.read(full_path)

if config.has_section("telemetry") and "uuid" in config["telemetry"]:
try:
return uuid.UUID(config["telemetry"]["uuid"]).hex
except ValueError:
pass # Invalid UUID found, will generate a new one

# Generate a new UUID and save it to the config file
if not config.has_section("telemetry"):
config.add_section("telemetry")
new_uuid = uuid.uuid4().hex
config.set("telemetry", "uuid", new_uuid)

os.makedirs(config_path, exist_ok=True)
with open(full_path, "w") as configfile:
config.write(configfile)

return new_uuid

except Exception as e:
logging.error(f"Failed to get or create UUID: {e}")
return ""


class KedroTelemetryCLIHooks:
Expand Down
12 changes: 8 additions & 4 deletions kedro-telemetry/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,17 @@ def test_before_command_run_empty_args(self, mocker, fake_metadata):
mocked_anon_id = mocker.patch("kedro_telemetry.plugin._hash")
mocked_anon_id.return_value = "digested"
mocker.patch("kedro_telemetry.plugin.PACKAGE_NAME", "spaceflights")
mocker.patch(
"kedro_telemetry.plugin._get_or_create_uuid",
return_value="hashed_username",
)

mocked_heap_call = mocker.patch("kedro_telemetry.plugin._send_heap_event")
telemetry_hook = KedroTelemetryCLIHooks()
command_args = []
telemetry_hook.before_command_run(fake_metadata, command_args)
expected_properties = {
"username": "digested",
"username": "hashed_username",
"package_name": "digested",
"project_version": kedro_version,
"telemetry_version": TELEMETRY_VERSION,
Expand All @@ -249,12 +253,12 @@ def test_before_command_run_empty_args(self, mocker, fake_metadata):
expected_calls = [
mocker.call(
event_name="Command run: kedro",
identity="digested",
identity="hashed_username",
properties=expected_properties,
),
mocker.call(
event_name="CLI command",
identity="digested",
identity="hashed_username",
properties=generic_properties,
),
]
Expand Down Expand Up @@ -296,7 +300,7 @@ def test_before_command_run_anonymous(self, mocker, fake_metadata):
mocked_anon_id = mocker.patch("kedro_telemetry.plugin._hash")
mocked_anon_id.return_value = "digested"
mocker.patch("kedro_telemetry.plugin.PACKAGE_NAME", "spaceflights")
mocker.patch("getpass.getuser", side_effect=Exception)
mocker.patch("kedro_telemetry.plugin.user_config_dir", side_effect=Exception)

mocked_heap_call = mocker.patch("kedro_telemetry.plugin._send_heap_event")
telemetry_hook = KedroTelemetryCLIHooks()
Expand Down

0 comments on commit a17da4a

Please sign in to comment.