1#!/usr/bin/env python3
2#
3# Copyright 2019, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Unit tests for inode2filename module.
20
21Install:
22  $> sudo apt-get install python3-pytest   ##  OR
23  $> pip install -U pytest
24See also https://docs.pytest.org/en/latest/getting-started.html
25
26Usage:
27  $> ./inode2filename_test.py
28  $> pytest inode2filename_test.py
29  $> python -m pytest inode2filename_test.py
30
31See also https://docs.pytest.org/en/latest/usage.html
32"""
33
34# global imports
35from contextlib import contextmanager
36import io
37import shlex
38import sys
39import typing
40
41# pip imports
42import pytest
43
44# local imports
45from inode2filename import *
46
47def create_inode2filename(*contents):
48  buf = io.StringIO()
49
50  for c in contents:
51    buf.write(c)
52    buf.write("\n")
53
54  buf.seek(0)
55
56  i2f = Inode2Filename(buf)
57
58  buf.close()
59
60  return i2f
61
62def test_inode2filename():
63  a = create_inode2filename("")
64  assert len(a) == 0
65  assert a.resolve(1, 2) == None
66
67  a = create_inode2filename("1 2 3 foo.bar")
68  assert len(a) == 1
69  assert a.resolve(1, 2) == "foo.bar"
70  assert a.resolve(4, 5) == None
71
72  a = create_inode2filename("1 2 3 foo.bar", "4 5 6 bar.baz")
73  assert len(a) == 2
74  assert a.resolve(1, 2) == "foo.bar"
75  assert a.resolve(4, 5) == "bar.baz"
76
77  a = create_inode2filename("1567d 8910 -1 /a/b/c/", "4 5 6 bar.baz")
78  assert len(a) == 2
79  assert a.resolve(1567, 8910) == "/a/b/c/"
80  assert a.resolve(4, 5) == "bar.baz"
81
82if __name__ == '__main__':
83  pytest.main()
84