Repository: PrefectHQ/prefect

Issue:
Client is creating .prefect config in current directory
Needs to be created in the home directory and not in a directory called `~` in the current directory


Discussion:

Resolving patch:
diff --git a/src/prefect/client.py b/src/prefect/client.py
--- a/src/prefect/client.py
+++ b/src/prefect/client.py
@@ -53,8 +53,9 @@ def __init__(self, token: str = None) -> None:
             token = prefect.config.cloud.get("auth_token", None)
 
             if token is None:
-                if os.path.exists("~/.prefect/.credentials/auth_token"):
-                    with open("~/.prefect/.credentials/auth_token", "r") as f:
+                token_path = os.path.expanduser("~/.prefect/.credentials/auth_token")
+                if os.path.exists(token_path):
+                    with open(token_path, "r") as f:
                         token = f.read()
 
         self.token = token
@@ -221,16 +222,19 @@ def login(
             raise ValueError("Could not log in.")
         self.token = response.json().get("token")
         if self.token:
-            if not os.path.exists("~/.prefect/.credentials"):
-                os.makedirs("~/.prefect/.credentials")
-            with open("~/.prefect/.credentials/auth_token", "w+") as f:
+            creds_path = os.path.expanduser("~/.prefect/.credentials")
+            if not os.path.exists(creds_path):
+                os.makedirs(creds_path)
+            with open(os.path.join(creds_path, "auth_token"), "w+") as f:
                 f.write(self.token)
 
     def logout(self) -> None:
         """
         Logs out by clearing all tokens, including deleting `~/.prefect/credentials/auth_token`
         """
-        os.remove("~/.prefect/.credentials/auth_token")
+        token_path = os.path.expanduser("~/.prefect/.credentials/auth_token")
+        if os.path.exists(token_path):
+            os.remove(token_path)
         del self.token
 
     def refresh_token(self) -> None:


Test patch:
diff --git a/tests/test_client.py b/tests/test_client.py
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -65,7 +65,10 @@ def test_client_logs_in_and_saves_token(monkeypatch):
     assert post.call_args[0][0] == "http://my-cloud.foo/login"
     assert post.call_args[1]["auth"] == ("test@example.com", "1234")
     assert client.token == "secrettoken"
-    assert mock_file.call_args[0] == ("~/.prefect/.credentials/auth_token", "w+")
+    assert mock_file.call_args[0] == (
+        os.path.expanduser("~/.prefect/.credentials/auth_token"),
+        "w+",
+    )
 
 
 def test_client_logs_in_from_config_credentials(monkeypatch):
@@ -87,7 +90,10 @@ def test_client_logs_in_from_config_credentials(monkeypatch):
     assert post.call_args[0][0] == "http://my-cloud.foo/login"
     assert post.call_args[1]["auth"] == ("test@example.com", "1234")
     assert client.token == "secrettoken"
-    assert mock_file.call_args[0] == ("~/.prefect/.credentials/auth_token", "w+")
+    assert mock_file.call_args[0] == (
+        os.path.expanduser("~/.prefect/.credentials/auth_token"),
+        "w+",
+    )
 
 
 def test_client_logs_out_and_deletes_auth_token(monkeypatch):
@@ -100,11 +106,12 @@ def test_client_logs_out_and_deletes_auth_token(monkeypatch):
     with set_temporary_config("cloud.api", "http://my-cloud.foo"):
         client = Client()
     client.login("test@example.com", "1234")
-    assert os.path.exists("~/.prefect/.credentials/auth_token")
-    with open("~/.prefect/.credentials/auth_token", "r") as f:
+    token_path = os.path.expanduser("~/.prefect/.credentials/auth_token")
+    assert os.path.exists(token_path)
+    with open(token_path, "r") as f:
         assert f.read() == "secrettoken"
     client.logout()
-    assert not os.path.exists("~/.prefect/.credentials/auth_token")
+    assert not os.path.exists(token_path)
 
 
 def test_client_raises_if_login_fails(monkeypatch):