Coverage for tests/env_test.py: 97%

37 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-02 23:32 +0000

1# 

2# -------------------------------------------------------------------------------- 

3# SPDX-FileCopyrightText: 2024-2025 Martin Jan Köhler and Harald Pretl 

4# Johannes Kepler University, Institute for Integrated Circuits. 

5# 

6# This file is part of KPEX  

7# (see https://github.com/iic-jku/klayout-pex). 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# SPDX-License-Identifier: GPL-3.0-or-later 

22# -------------------------------------------------------------------------------- 

23# 

24 

25from __future__ import annotations 

26 

27import allure 

28import os 

29from typing import * 

30import unittest 

31from unittest import mock 

32 

33from klayout_pex.env import Env, EnvVar 

34from klayout_pex.pdk_config import PDK 

35 

36 

37@allure.parent_suite("Unit Tests") 

38@allure.tag("Env", "Environment", "Environmental Variables") 

39# NOTE: each test restores the environment, so its variables don't leak into later tests 

40@mock.patch.dict(os.environ) 

41class Test(unittest.TestCase): 

42 def test_env_has_defaults(self): 

43 # ensure env is unset 

44 for var in EnvVar: 

45 if var.value in os.environ: 

46 del os.environ[var.value] 

47 

48 env = Env.from_os_environ() 

49 

50 for var in EnvVar: 

51 val = env[var] 

52 self.assertNotEqual('', val, f"Env must have a non-empty default for every variable, " 

53 f"but {var.value} has none!") 

54 

55 def test_env_with_custom_variables(self): 

56 def value_for_var(var: EnvVar) -> str: 

57 return f"{var.value}_is_set" 

58 

59 # ensure env is unset 

60 for var in EnvVar: 

61 os.environ[var.value] = value_for_var(var) 

62 

63 env = Env.from_os_environ() 

64 

65 for var in EnvVar: 

66 val = env[var] 

67 self.assertEqual(value_for_var(var), val, 

68 f"Envrionmental variable {var.value} was set to '{value_for_var(var)}', " 

69 f"but Env['{var.value}'] returns '{val}'!") 

70 

71 def test_valid_pdk_from_env(self): 

72 os.environ[EnvVar.PDK.value] = 'sky130A' 

73 env = Env.from_os_environ() 

74 self.assertEqual(PDK.SKY130A, env.default_pdk) 

75 

76 def test_invalid_pdk_from_env(self): 

77 os.environ[EnvVar.PDK.value] = 'invalid_pdk' 

78 env = Env.from_os_environ() 

79 self.assertIsNone(env.default_pdk)