From 20b0f680a65acbd9517c847e04ff0c16a1e77290 Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Thu, 21 Feb 2019 20:44:33 -0700 Subject: [PATCH 1/2] Add cwd grain for working directory --- salt/grains/core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/salt/grains/core.py b/salt/grains/core.py index 04c1ae91b5f5..34547b46b4c7 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2403,6 +2403,13 @@ def get_machine_id(): return {'machine_id': machineid.read().strip()} +def cwd(): + ''' + Current working directory + ''' + return {'cwd': os.getcwd()} + + def path(): ''' Return the path From 304f6bc13aa4abec69e95ee23fcced7fd506fef5 Mon Sep 17 00:00:00 2001 From: emoncada Date: Thu, 21 Nov 2019 13:54:39 -0700 Subject: [PATCH 2/2] Test 51758 --- tests/unit/grains/test_core.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py index 8c45ee194abd..2d3fac62d6a0 100644 --- a/tests/unit/grains/test_core.py +++ b/tests/unit/grains/test_core.py @@ -1241,3 +1241,25 @@ def test_locale_info_no_tz_tzname(self): is_proxy.assert_called_once_with() is_windows.assert_not_called() self.assertEqual(ret['locale_info']['timezone'], 'unknown') + + def test_cwd_exists(self): + cwd_grain = core.cwd() + + self.assertIsInstance(cwd_grain, dict) + self.assertTrue('cwd' in cwd_grain) + self.assertEqual(cwd_grain['cwd'], os.getcwd()) + + def test_cwd_is_cwd(self): + cwd = os.getcwd() + + try: + # change directory + new_dir = os.path.split(cwd)[0] + os.chdir(new_dir) + + cwd_grain = core.cwd() + + self.assertEqual(cwd_grain['cwd'], new_dir) + finally: + # change back to original directory + os.chdir(cwd)