1+ import os
12from pathlib import Path
23from unittest .mock import ANY , Mock , patch
34
45import pytest
56
7+ from hapless .hap import Hap
68from hapless .main import Hapless
79
810
@@ -22,19 +24,37 @@ def test_get_hap_dirs_with_hap(hapless: Hapless, hap):
2224
2325
2426def test_create_hap (hapless : Hapless ):
25- result = hapless .create_hap ("echo hello " )
26- assert result .cmd == "echo hello "
27- assert result .hid == "1"
28- assert result .name is not None
29- assert isinstance (result .name , str )
30- assert result .name .startswith ("hap-" )
27+ hap = hapless .create_hap ("echo create " )
28+ assert hap .cmd == "echo create "
29+ assert hap .hid == "1"
30+ assert hap .name is not None
31+ assert isinstance (hap .name , str )
32+ assert hap .name .startswith ("hap-" )
3133
3234
3335def test_create_hap_custom_hid (hapless : Hapless ):
34- result = hapless .create_hap (cmd = "echo hello" , hid = "42" , name = "hap-name" )
35- assert result .cmd == "echo hello"
36- assert result .hid == "42"
37- assert result .name == "hap-name"
36+ hap = hapless .create_hap (cmd = "echo hid" , hid = "42" , name = "hap-name" )
37+ assert hap .cmd == "echo hid"
38+ assert hap .hid == "42"
39+ assert hap .name == "hap-name"
40+
41+
42+ @patch .dict (os .environ , {"ENV_KEY" : "TEST_VALUE" }, clear = True )
43+ def test_create_hap_defaults_to_current_env (hapless : Hapless ):
44+ env = {"ENV_KEY" : "TEST_VALUE" }
45+
46+ with patch (
47+ "hapless.main.Hap._set_env" ,
48+ autospec = True ,
49+ side_effect = Hap ._set_env ,
50+ ) as set_env_mock :
51+ hap = hapless .create_hap (cmd = "echo env" , name = "hap-env" )
52+
53+ # NOTE: it's a method, so we need to pass `self` as the first argument
54+ set_env_mock .assert_called_once_with (hap , env )
55+ assert hap .cmd == "echo env"
56+ assert hap .name == "hap-env"
57+ assert hap .env == env
3858
3959
4060def test_get_hap_works_with_restarts (hapless : Hapless ):
0 commit comments