1#!/usr/bin/env python3
2# Copyright (c) 2020 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5
6"""tests for subfolder_list.py"""
7
8import os
9import sys
10import time
11
12sys.path.insert(0, os.path.join(os.environ["ZEPHYR_BASE"], "scripts"))
13import subfolder_list as iut  # Implementation Under Test
14
15OUT_FILE = "out_file.txt"
16DIR_NAME_PREFIX = "dir-"
17
18
19def mkdirs(name_sfxs_range):
20    """Create directories"""
21    dir_names = [os.getcwd()]
22
23    for sfx in name_sfxs_range:
24        name = f"{DIR_NAME_PREFIX}{sfx}"
25        os.mkdir(name)
26        dir_names.append(os.path.join(os.getcwd(), name))
27
28    return dir_names
29
30
31def assert_out_file_has(dir_names):
32    """Assert that out file has names of directories"""
33    for dir_name in open(OUT_FILE).readlines():
34        dir_name = dir_name.strip()
35        assert dir_name in dir_names
36
37
38def test_subfolder_list(tmpdir):
39    """Test subfolder list is correct"""
40    tmpdir.chdir()
41    dir_names = mkdirs(range(5))
42    iut_dir_names = iut.get_subfolder_list((str(tmpdir)))
43    assert dir_names == iut_dir_names
44
45
46def test_links(tmpdir):
47    """Test directory links creation"""
48    tmpdir.chdir()
49    links_dir = str(tmpdir)
50    dirs_dir = "dirs"
51    subdirs_parent_sfx = 1
52
53    dirs_range = range(5)
54    subdirs_range = range(5, 9)
55
56    expect_links = []
57    for i in dirs_range:
58        expect_links.append("%s_%s%d" % (dirs_dir, DIR_NAME_PREFIX, i))
59    for i in subdirs_range:
60        expect_links.append("%s_%s%d_%s%d" % (
61            dirs_dir, DIR_NAME_PREFIX, subdirs_parent_sfx, DIR_NAME_PREFIX, i))
62
63    tmpdir.mkdir(dirs_dir)
64    os.chdir(dirs_dir)
65    mkdirs(dirs_range)
66
67    os.chdir(f"{DIR_NAME_PREFIX}{subdirs_parent_sfx}")
68    mkdirs(subdirs_range)
69    tmpdir.chdir()
70
71    iut.get_subfolder_list(dirs_dir, links_dir)
72
73    links = [f for f in os.listdir(links_dir) if os.path.islink(f)]
74    assert sorted(expect_links) == sorted(links)
75
76
77def test_gen_out_file(tmpdir):
78    """Test generating the list output file"""
79    tmpdir.chdir()
80    dirs = []
81    for sfx in range(10):
82        dirs.append(f"{DIR_NAME_PREFIX}{sfx}")
83
84    iut.gen_out_file(OUT_FILE, dirs)
85    assert_out_file_has(dirs)
86    st_info = os.stat(OUT_FILE)
87
88    # should not be updated if it already exists and has the same content
89    iut.gen_out_file(OUT_FILE, dirs)
90    st_info2 = os.stat(OUT_FILE)
91    assert st_info == st_info2
92
93    # should be updated if exists with different content
94    with open(OUT_FILE, "a") as out_fo:
95        out_fo.write("A" * 79)
96    st_info = os.stat(OUT_FILE)
97    iut.gen_out_file(OUT_FILE, dirs)
98    st_info2 = os.stat(OUT_FILE)
99    assert st_info != st_info2
100    assert_out_file_has(dirs)
101
102
103def test_trigger_file(tmpdir):
104    """Test trigger file feature"""
105    trigger_file = "trigger_file"
106    tmpdir.chdir()
107    mkdirs(range(5))
108
109    # should be created if it does not exist
110    iut.touch(trigger_file)
111    assert os.path.exists(trigger_file)
112
113    # should be touched if it exists
114    with open(trigger_file, 'w'):
115        fake_time = 12345
116        os.utime(trigger_file, (fake_time, fake_time))
117
118    iut.touch(trigger_file)
119    st_info = os.stat(trigger_file)
120
121    time_now = time.time()
122    time_since_touch = 5.0  # seconds, rough estimate
123
124    assert (time_now - st_info.st_atime) <= time_since_touch
125    assert (time_now - st_info.st_mtime) <= time_since_touch
126