master
ブランチ master
LittleBeast / src / Std / Test / LibLogs.php
5.6 KB、129行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Test;
39
40require_once __DIR__.'/../../../autoload.php';
41
42use Std\Lib\Tester;
43use Std\Lib\Logs;
44
45$test = new Tester([
46 'colorOutput' => true,
47 'verboseOutput' => true
48]);
49
50$base = sys_get_temp_dir().'/lblogs_'.bin2hex(random_bytes(6));
51mkdir($base, 0700);
52
53$test->describe('ログ', function($test) use ($base): void {
54 $test->it('ディレクトリが無ければ空のはず', function($test) use ($base): void {
55 $logs = new Logs("{$base}/nothing");
56 $test->assertEquals([], $logs->list());
57 $test->assertNull($logs->tail('db_log.txt'));
58 $test->assertFalse($logs->clear('db_log.txt'));
59 });
60
61 $test->it('ファイルの一覧を出すはず(隠しファイル・ディレクトリを除く)', function($test) use ($base): void {
62 file_put_contents("{$base}/db_log.txt", "a\n");
63 file_put_contents("{$base}/auth_log.txt", "bb\n");
64 file_put_contents("{$base}/.hidden", "x");
65 mkdir("{$base}/sub");
66 $list = (new Logs($base))->list();
67 $test->assertEquals(['auth_log.txt', 'db_log.txt'], array_column($list, 'name'));
68 $test->assertEquals(3, $list[0]['size']);
69 });
70
71 $test->it('ディレクトリの外のファイルは読めないはず', function($test) use ($base): void {
72 $logs = new Logs("{$base}/sub");
73 file_put_contents("{$base}/sub/ok.txt", "x\n");
74 foreach (['../db_log.txt', '..', '.hidden', 'a/b', '', "ok.txt\0"] as $bad) {
75 $test->assertNull($logs->path($bad), $bad);
76 $test->assertNull($logs->tail($bad), $bad);
77 $test->assertFalse($logs->delete($bad), $bad);
78 }
79 $test->assertNotNull($logs->path('ok.txt'));
80 });
81
82 $test->it('最後の行を読むはず', function($test) use ($base): void {
83 $logs = new Logs($base);
84 $lines = [];
85 for ($i = 1; $i <= 30000; $i++) $lines[] = "行{$i} ".str_repeat('x', $i % 7);
86 file_put_contents("{$base}/big.txt", implode("\n", $lines)."\n");
87 $t = $logs->tail('big.txt', 3);
88 $test->assertEquals(['行29998 '.str_repeat('x', 29998 % 7), '行29999 '.str_repeat('x', 29999 % 7), '行30000 '.str_repeat('x', 30000 % 7)], $t['lines']);
89 $test->assertTrue($t['more']);
90 $t = $logs->tail('big.txt', 5000);
91 $test->assertCount(5000, $t['lines']);
92 $test->assertEquals('行25001 '.str_repeat('x', 25001 % 7), $t['lines'][0]);
93
94 // 最後の改行が無い、CRLF、短いファイル
95 file_put_contents("{$base}/crlf.txt", "a\r\nb\r\nc");
96 $t = $logs->tail('crlf.txt', 10);
97 $test->assertEquals(['a', 'b', 'c'], $t['lines']);
98 $test->assertFalse($t['more']);
99 file_put_contents("{$base}/empty.txt", '');
100 $test->assertEquals(['lines' => [], 'more' => false], $logs->tail('empty.txt'));
101 });
102
103 $test->it('検索した行だけを読むはず', function($test) use ($base): void {
104 $logs = new Logs($base);
105 $t = $logs->tail('big.txt', 2, '行2999');
106 $test->assertEquals(['行29998 '.str_repeat('x', 29998 % 7), '行29999 '.str_repeat('x', 29999 % 7)], $t['lines']);
107 $test->assertTrue($t['more']);
108 file_put_contents("{$base}/case.txt", "Error 1\nok\nERROR 2\n");
109 $test->assertEquals(['Error 1', 'ERROR 2'], $logs->tail('case.txt', 10, 'error')['lines']);
110 $test->assertEquals([], $logs->tail('case.txt', 10, '無い')['lines']);
111 });
112
113 $test->it('空にして、削除出来るはず', function($test) use ($base): void {
114 $logs = new Logs($base);
115 $test->assertTrue($logs->clear('case.txt'));
116 clearstatcache();
117 $test->assertEquals(0, filesize("{$base}/case.txt"));
118 $test->assertTrue($logs->delete('case.txt'));
119 $test->assertFalse(is_file("{$base}/case.txt"));
120 $test->assertFalse($logs->delete('case.txt'));
121 });
122});
123
124foreach (glob("{$base}/sub/*") ?: [] as $f) @unlink($f);
125@rmdir("{$base}/sub");
126foreach (glob("{$base}/{,.}*", GLOB_BRACE) ?: [] as $f) if (is_file($f)) @unlink($f);
127@rmdir($base);
128
129$test->printSummary();