summaryrefslogtreecommitdiff
path: root/extra/usb_power/powerlog_unittest.py
blob: 62667e35b884f4ccc780822d27189c3aa540ba12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Copyright 2018 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for powerlog."""

import os
import shutil
import tempfile
import unittest

from usb_power import powerlog


class TestPowerlog(unittest.TestCase):
    """Test to verify powerlog util methods work as expected."""

    def setUp(self):
        """Set up data and create a temporary directory to save data and stats."""
        self.tempdir = tempfile.mkdtemp()
        self.filename = "testfile"
        self.filepath = os.path.join(self.tempdir, self.filename)
        with open(self.filepath, "w") as f:
            f.write("")

    def tearDown(self):
        """Delete the temporary directory and its content."""
        shutil.rmtree(self.tempdir)

    def test_ProcessFilenameAbsoluteFilePath(self):
        """Absolute file path is returned unchanged."""
        processed_fname = powerlog.process_filename(self.filepath)
        self.assertEqual(self.filepath, processed_fname)

    def test_ProcessFilenameRelativeFilePath(self):
        """Finds relative file path inside a known config location."""
        original = powerlog.CONFIG_LOCATIONS
        powerlog.CONFIG_LOCATIONS = [self.tempdir]
        processed_fname = powerlog.process_filename(self.filename)
        try:
            self.assertEqual(self.filepath, processed_fname)
        finally:
            powerlog.CONFIG_LOCATIONS = original

    def test_ProcessFilenameInvalid(self):
        """IOError is raised when file cannot be found by any of the four ways."""
        with self.assertRaises(IOError):
            powerlog.process_filename(self.filename)


if __name__ == "__main__":
    unittest.main()