1#!/usr/bin/env python3 2# Copyright 2020 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests the python_packages module.""" 16 17import importlib.metadata 18from pathlib import Path 19import tempfile 20import unittest 21from unittest import mock 22 23from pw_env_setup import python_packages 24 25 26class TestPythonPackages(unittest.TestCase): 27 """Tests the python_packages module.""" 28 29 def setUp(self): 30 self.existing_pkgs_minus_toml = '\n'.join( 31 pkg 32 for pkg in python_packages._installed_packages() # pylint: disable=protected-access 33 if not pkg.startswith('toml==') 34 ) 35 self.temp_dir = tempfile.TemporaryDirectory() 36 self.temp_path = Path(self.temp_dir.name) 37 38 def tearDown(self): 39 self.temp_dir.cleanup() 40 41 def test_list(self): 42 # pylint: disable=protected-access 43 pkgs = list(python_packages._installed_packages()) 44 # pylint: enable=protected-access 45 toml_version = importlib.metadata.version('toml') 46 47 self.assertIn(f'toml=={toml_version}', pkgs) 48 self.assertNotIn('pw-foo', pkgs) 49 50 @mock.patch('pw_env_setup.python_packages._stderr') 51 def test_diff_removed(self, stderr_mock): 52 expected = 'foo==1.0\nbar==2.0\nbaz==3.0\n' 53 expected_file = self.temp_path / 'test_diff_removed_expected' 54 expected_file.write_text(expected, encoding='utf-8') 55 56 # Removed packages should trigger a failure. 57 self.assertEqual(-1, python_packages.diff(expected_file)) 58 59 stderr_mock.assert_any_call('Removed packages') 60 stderr_mock.assert_any_call(' foo==1.0') 61 stderr_mock.assert_any_call(' bar==2.0') 62 stderr_mock.assert_any_call(' baz==3.0') 63 64 @mock.patch('pw_env_setup.python_packages._stderr') 65 def test_diff_updated(self, stderr_mock): 66 expected = 'toml>=0.0.1\n' + self.existing_pkgs_minus_toml 67 expected_file = self.temp_path / 'test_diff_updated_expected' 68 expected_file.write_text(expected, encoding='utf-8') 69 70 toml_version = importlib.metadata.version('toml') 71 72 # Updated packages should trigger a failure. 73 self.assertEqual(-1, python_packages.diff(expected_file)) 74 75 stderr_mock.assert_any_call('Updated packages') 76 stderr_mock.assert_any_call( 77 f' toml=={toml_version} (from toml>=0.0.1)' 78 ) 79 80 @mock.patch('pw_env_setup.python_packages._stderr') 81 def test_diff_new(self, stderr_mock): 82 expected = self.existing_pkgs_minus_toml 83 expected_file = self.temp_path / 'test_diff_new_expected' 84 expected_file.write_text(expected, encoding='utf-8') 85 86 toml_version = importlib.metadata.version('toml') 87 88 # New packages should trigger a failure. 89 self.assertEqual(-1, python_packages.diff(expected_file)) 90 91 stderr_mock.assert_any_call('New packages') 92 stderr_mock.assert_any_call(f' toml=={toml_version}') 93 stderr_mock.assert_any_call("Package versions don't match!") 94 95 96if __name__ == '__main__': 97 unittest.main() 98