main
py 109 lines 3.67 KB
Raw
1 # Copyright 2026 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16 import os
17 import shutil
18 import sys
19 import tempfile
20 import unittest
21 import pytest
22 from unittest.mock import patch
23 from colab_cli.cli import main
24 from colab_cli.history import HistoryLogger
25
26
27 class TestLogExport(unittest.TestCase):
28 def setUp(self):
29 self.temp_dir = tempfile.mkdtemp()
30 self.history_dir = os.path.join(self.temp_dir, "history")
31 os.makedirs(self.history_dir)
32 self.session_name = "test-export"
33 self.log_path = os.path.join(self.history_dir, f"{self.session_name}.jsonl")
34
35 # Sample events
36 events = [
37 {
38 "timestamp": "2026-03-23T12:00:00.000000+00:00",
39 "event_type": "session_created",
40 "endpoint": "ep1",
41 "accelerator": "NONE",
42 },
43 {
44 "timestamp": "2026-03-23T12:01:00.000000+00:00",
45 "event_type": "execution",
46 "code": "print(1)",
47 "outputs": [{"text": "1\n"}],
48 },
49 {
50 "timestamp": "2026-03-23T12:02:00.000000+00:00",
51 "event_type": "file_operation",
52 "op": "ls",
53 "path": "content",
54 },
55 ]
56 with open(self.log_path, "w", encoding="utf-8") as f:
57 for event in events:
58 f.write(json.dumps(event) + "\n")
59
60 def tearDown(self):
61 shutil.rmtree(self.temp_dir)
62 if os.path.exists(f"{self.session_name}.ipynb"):
63 os.remove(f"{self.session_name}.ipynb")
64
65 @patch("colab_cli.commands.utility.state")
66 def test_log_export(self, mock_state):
67 # Setup mocks to return our test events
68
69 # Real HistoryLogger to read our temp log
70 real_history = HistoryLogger(log_dir=self.history_dir)
71 mock_state.history.get_history.side_effect = real_history.get_history
72
73 with patch.object(
74 sys,
75 "argv",
76 [
77 "colab",
78 "log",
79 "-s",
80 self.session_name,
81 "-o",
82 f"{self.session_name}.ipynb",
83 ],
84 ):
85 with pytest.raises(SystemExit) as exitinfo:
86 main()
87 self.assertEqual(exitinfo.value.code, 0)
88
89 output_file = f"{self.session_name}.ipynb"
90 self.assertTrue(os.path.exists(output_file))
91
92 with open(output_file, "r") as f:
93 nb = json.load(f)
94 # Should have title, session_created md, execution code, and file_op md cells
95 # Total cells: 4 (title, session_created, execution, file_op)
96 self.assertEqual(len(nb["cells"]), 4)
97 self.assertEqual(nb["cells"][2]["cell_type"], "code")
98 source = nb["cells"][2]["source"]
99 if isinstance(source, list):
100 source = "".join(source)
101 self.assertEqual(source, "print(1)")
102 output_text = nb["cells"][2]["outputs"][0]["text"]
103 if isinstance(output_text, list):
104 output_text = "".join(output_text)
105 self.assertEqual(output_text, "1\n")
106
107
108 if __name__ == "__main__":
109 unittest.main()