# Path: ubuntucleaner/utils/system.py
# def get_codename():
#     return os.popen('lsb_release -cs').read().strip()
# 
# def get_desktop():
#     '''
#     ubuntu
#     ubuntu-2d
#     gnome-classic
#     gnome-shell
#     '''
#     return os.getenv('DESKTOP_SESSION')
# 
# def get_distro():
#     return os.popen('lsb_release -ds').read().strip()


import unittest
import mock
from ubuntucleaner.utils.system import get_codename, get_desktop, get_distro

def patch_get_from_name():
    return mock.patch("ubuntucleaner.utils.icon.get_from_name")


class TestSystemModule(unittest.TestCase):

    def test_get_distro(self):
        with mock.patch("ubuntucleaner.utils.system.os.popen") as m_popen:
            m_popen.return_value.read.return_value = 'Ubuntu 18.04.4 LTS\n'
            distro = get_distro()
        self.assertEqual(
            m_popen.call_args_list, [mock.call('lsb_release -ds')]
        )
        self.assertEqual(distro, 'Ubuntu 18.04.4 LTS')

    def test_get_codename(self):
        """The codename is retrieved executing a `lsb_release` via `popen()`."""
        with mock.patch("ubuntucleaner.utils.system.os.popen") as m_popen:
            m_popen.return_value.read.return_value = 'bionic\n'
            codename = get_codename()
        self.assertEqual(
            m_popen.call_args_list, [mock.call('lsb_release -cs')]
        )
        self.assertEqual(codename, 'bionic')

    def test_get_desktop(self):
        """Desktop is retrieved from environment variable."""
        environ = {"DESKTOP_SESSION": "ubuntu"}
        with mock.patch.dict("ubuntucleaner.utils.system.os.environ", environ):

# Path: ubuntucleaner/utils/system.py
# def get_codename():
#     return os.popen('lsb_release -cs').read().strip()
# 
# def get_desktop():
#     '''
#     ubuntu
#     ubuntu-2d
#     gnome-classic
#     gnome-shell
#     '''
#     return os.getenv('DESKTOP_SESSION')
# 
# def get_distro():
#     return os.popen('lsb_release -ds').read().strip()


# Path: tests/utils/test_system.py
import unittest
import mock
from ubuntucleaner.utils.system import get_codename, get_desktop, get_distro


def patch_get_from_name():
    return mock.patch("ubuntucleaner.utils.icon.get_from_name")


class TestSystemModule(unittest.TestCase):

    def test_get_distro(self):
        with mock.patch("ubuntucleaner.utils.system.os.popen") as m_popen:
            m_popen.return_value.read.return_value = 'Ubuntu 18.04.4 LTS\n'
            distro = get_distro()
        self.assertEqual(
            m_popen.call_args_list, [mock.call('lsb_release -ds')]
        )
        self.assertEqual(distro, 'Ubuntu 18.04.4 LTS')

    def test_get_codename(self):
        """The codename is retrieved executing a `lsb_release` via `popen()`."""
        with mock.patch("ubuntucleaner.utils.system.os.popen") as m_popen:
            m_popen.return_value.read.return_value = 'bionic\n'
            codename = get_codename()
        self.assertEqual(
            m_popen.call_args_list, [mock.call('lsb_release -cs')]
        )
        self.assertEqual(codename, 'bionic')

    def test_get_desktop(self):
        """Desktop is retrieved from environment variable."""
        environ = {"DESKTOP_SESSION": "ubuntu"}
        with mock.patch.dict("ubuntucleaner.utils.system.os.environ", environ):

            desktop = get_desktop()