summaryrefslogtreecommitdiff
path: root/extra/usb_power/powerlog_unittest.py
blob: 1d0718530e3880ab22030b6c14b0234c0eed24a9 (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
53
54
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Ignore indention messages, since legacy scripts use 2 spaces instead of 4.
# pylint: disable=bad-indentation,docstring-section-indent
# pylint: disable=docstring-trailing-quotes

"""Unit tests for powerlog."""

import os
import shutil
import tempfile
import unittest

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()