Skip to content

Commit ce96823

Browse files
committed
添加完整的双语注释和测试兼容性优化
- 给所有导出函数和类型添加双语注释(英文+中文) - 优化 Firefox 和 w3m 测试的 CI 环境兼容性 - 添加测试函数注释说明跳过实际浏览器打开的原因 - 确保所有包级别和关键函数都有详细的功能说明
1 parent 1f81498 commit ce96823

File tree

8 files changed

+212
-2
lines changed

8 files changed

+212
-2
lines changed

firefoxopen/firefox_open_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
11
package firefoxopen
2+
3+
import (
4+
"os/exec"
5+
"testing"
6+
)
7+
8+
// TestOpen verifies Firefox availability without opening browser
9+
// Checks Firefox command availability for CI compatibility testing
10+
// Skips actual browser opening to avoid disrupting test automation
11+
//
12+
// TestOpen 验证 Firefox 可用性而不打开浏览器
13+
// 检查 Firefox 命令可用性以进行 CI 兼容性测试
14+
// 跳过实际的浏览器打开以避免干扰测试自动化
15+
func TestOpen(t *testing.T) {
16+
path, err := exec.LookPath("firefox")
17+
if err != nil {
18+
t.Skip("firefox is not available on this system, skipping test case")
19+
}
20+
t.Log("firefox path:", path)
21+
t.Log("skipping open firefox to avoid interrupting testcase...")
22+
}

firefoxopen/firefox_show.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ import (
1010
"github.com/yyle88/osexec"
1111
)
1212

13+
// FirefoxShow manages Firefox browser automation with webpage output control
14+
// Provides coordinated webpage generation and Firefox opening with timing control
15+
// Handles temporary HTTP service and browser window management
16+
//
17+
// FirefoxShow 管理 Firefox 浏览器自动化与网页输出控制
18+
// 提供协调的网页生成和 Firefox 打开及时间控制
19+
// 处理临时 HTTP 服务和浏览器窗口管理
1320
type FirefoxShow struct {
1421
service *gintestpage.Service
1522
command *osexec.OsCommand
1623
showTime time.Time
1724
doneOnce *sync.Once
1825
}
1926

27+
// NewFirefoxShow creates Firefox automation instance with custom service and command
28+
// Initializes browser automation using provided HTTP service and OS command configuration
29+
// Returns configured Firefox automation handler for coordinated browser operations
30+
//
31+
// NewFirefoxShow 使用自定义服务和命令创建 Firefox 自动化实例
32+
// 使用提供的 HTTP 服务和操作系统命令配置初始化浏览器自动化
33+
// 返回配置的 Firefox 自动化处理器以实现协调的浏览器操作
2034
func NewFirefoxShow(service *gintestpage.Service, command *osexec.OsCommand) *FirefoxShow {
2135
return &FirefoxShow{
2236
service: service,
@@ -26,23 +40,55 @@ func NewFirefoxShow(service *gintestpage.Service, command *osexec.OsCommand) *Fi
2640
}
2741
}
2842

43+
// NewFirefoxDraw creates Firefox automation with default configuration for development
44+
// Initializes browser automation using default HTTP test service and debug command setup
45+
// Returns ready-to-use Firefox automation handler with optimal development settings
46+
//
47+
// NewFirefoxDraw 使用开发的默认配置创建 Firefox 自动化
48+
// 使用默认 HTTP 测试服务和调试命令设置初始化浏览器自动化
49+
// 返回具有优化开发设置的即用 Firefox 自动化处理器
2950
func NewFirefoxDraw() *FirefoxShow {
3051
return NewFirefoxShow(gintestpage.NewService(), osexec.NewOsCommand().WithDebug())
3152
}
3253

54+
// Close terminates Firefox automation after specified wait time
55+
// Waits for remaining time based on when pages were shown and closes HTTP service
56+
// Uses sync.Once to ensure cleanup happens exactly once
57+
//
58+
// Close 在指定等待时间后终止 Firefox 自动化
59+
// 根据页面显示时间等待剩余时间并关闭 HTTP 服务
60+
// 使用 sync.Once 确保清理操作仅执行一次
3361
func (op *FirefoxShow) Close(waitTime time.Duration) {
3462
op.doneOnce.Do(func() {
3563
time.Sleep(waitTime - time.Since(op.showTime))
3664
op.service.Close()
3765
})
3866
}
3967

68+
// ShowInNewWindows displays HTML pages in separate Firefox windows
69+
// Opens each page in its own Firefox window for independent viewing
70+
// Creates temporary URLs and launches Firefox with --new-window option
71+
//
72+
// ShowInNewWindows 在单独的 Firefox 窗口中显示 HTML 页面
73+
// 在独立的 Firefox 窗口中打开每个页面以供独立查看
74+
// 创建临时 URL 并使用 --new-window 选项启动 Firefox
4075
func (op *FirefoxShow) ShowInNewWindows(pages ...string) {
41-
op.Show(pages, "--new-window") //打开若干个新窗口以打开若干个网页
76+
op.Open(pages, "--new-window") //打开若干个新窗口以打开若干个网页
4277
}
4378

79+
// ShowInNewTabs displays HTML pages in new Firefox tabs
80+
// Opens each page in a new tab within existing or new Firefox window
81+
// Creates temporary URLs and launches Firefox with --new-tab option
82+
//
83+
// ShowInNewTabs 在新的 Firefox 标签中显示 HTML 页面
84+
// 在现有或新的 Firefox 窗口的新标签中打开每个页面
85+
// 创建临时 URL 并使用 --new-tab 选项启动 Firefox
4486
func (op *FirefoxShow) ShowInNewTabs(pages ...string) {
45-
op.Show(pages, "--new-tab") //打开若干个新标签以打开若干个网页
87+
op.Open(pages, "--new-tab") //打开若干个新标签以打开若干个网页
88+
}
89+
90+
func (op *FirefoxShow) Open(pages []string, openOption string) {
91+
op.Show(pages, openOption)
4692
}
4793

4894
func (op *FirefoxShow) Show(pages []string, openOption string) {

firefoxopen/firefox_show_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
11
package firefoxopen
2+
3+
import (
4+
"os/exec"
5+
"testing"
6+
)
7+
8+
// TestFirefoxShow_Open verifies Firefox availability without opening browser
9+
// Checks Firefox command availability for CI compatibility testing
10+
// Skips actual browser opening to avoid disrupting test automation
11+
//
12+
// TestFirefoxShow_Open 验证 Firefox 可用性而不打开浏览器
13+
// 检查 Firefox 命令可用性以进行 CI 兼容性测试
14+
// 跳过实际的浏览器打开以避免干扰测试自动化
15+
func TestFirefoxShow_Open(t *testing.T) {
16+
path, err := exec.LookPath("firefox")
17+
if err != nil {
18+
t.Skip("firefox is not available on this system, skipping test case")
19+
}
20+
t.Log("firefox path:", path)
21+
t.Log("skipping open firefox to avoid interrupting testcase...")
22+
}

gintestpage/gin_test_page.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@ import (
2121
"github.com/yyle88/syncmap"
2222
)
2323

24+
// Service provides HTTP test server functionality with dynamic page hosting
25+
// Manages temporary HTTP server and in-memory page storage for testing
26+
// Enables rapid webpage content serving with unique URL generation
27+
//
28+
// Service 提供带有动态页面托管的 HTTP 测试服务器功能
29+
// 管理临时 HTTP 服务器和内存中的页面存储以供测试
30+
// 启用具有唯一 URL 生成的快速网页内容服务
2431
type Service struct {
2532
srvTest *httptest.Server
2633
pageMap *syncmap.Map[string, []byte]
2734
}
2835

36+
// NewService creates HTTP test server instance with Gin framework integration
37+
// Initializes temporary HTTP server with dynamic page routing capabilities
38+
// Returns configured service ready for webpage hosting and URL generation
39+
//
40+
// NewService 创建与 Gin 框架集成的 HTTP 测试服务器实例
41+
// 初始化具有动态页面路由功能的临时 HTTP 服务器
42+
// 返回准备好进行网页托管和 URL 生成的配置服务
2943
func NewService() *Service {
3044
pageMap := syncmap.New[string, []byte]()
3145

@@ -47,16 +61,37 @@ func NewService() *Service {
4761
}
4862
}
4963

64+
// Close terminates HTTP test server and releases resources
65+
// Stops the temporary HTTP server and cleans up network resources
66+
// Should be called when testing is complete
67+
//
68+
// Close 终止 HTTP 测试服务器并释放资源
69+
// 停止临时 HTTP 服务器并清理网络资源
70+
// 应在测试完成时调用
5071
func (service *Service) Close() {
5172
service.srvTest.Close()
5273
}
5374

75+
// SetPage registers HTML page content at specified path and returns access URL
76+
// Stores page content in memory and generates accessible URL for the content
77+
// Path must not contain slash characters for security
78+
//
79+
// SetPage 在指定路径注册 HTML 页面内容并返回访问 URL
80+
// 将页面内容存储在内存中并为内容生成可访问的 URL
81+
// 出于安全考虑,路径不能包含斜杠字符
5482
func (service *Service) SetPage(path string, page []byte) string {
5583
muststrings.NotContains(must.Nice(path), "/")
5684
service.pageMap.Store(path, page)
5785
return service.GetLink(path)
5886
}
5987

88+
// GetLink generates complete URL for specified path on test server
89+
// Constructs full HTTP URL using test server base and provided path
90+
// Path must not contain slash characters for security
91+
//
92+
// GetLink 为测试服务器上的指定路径生成完整 URL
93+
// 使用测试服务器基础地址和提供的路径构建完整 HTTP URL
94+
// 出于安全考虑,路径不能包含斜杠字符
6095
func (service *Service) GetLink(path string) string {
6196
muststrings.NotContains(must.Nice(path), "/")
6297
return rese.C1(url.JoinPath(service.srvTest.URL, path))

w3mopenpage/w3m_open_page.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// Package w3mopenpage: W3m console browser automation for webpage output
2+
// Provides command-line webpage rendering using w3m text browser
3+
// Enables HTML content display in console environment with text-based output
4+
// Supports both URL opening and direct HTML content processing via w3m
5+
//
6+
// w3mopenpage: 用于网页输出的 W3m 控制台浏览器自动化
7+
// 使用 w3m 文本浏览器提供命令行网页渲染
8+
// 在控制台环境中启用 HTML 内容显示,提供基于文本的输出
9+
// 支持通过 w3m 进行 URL 打开和直接 HTML 内容处理
110
package w3mopenpage
211

312
import (
@@ -9,11 +18,25 @@ import (
918
"github.com/yyle88/zaplog"
1019
)
1120

21+
// Open displays webpage content from URL using w3m browser in console
22+
// Executes w3m with -dump option to render webpage as text output
23+
// Logs rendered webpage content for console-based webpage viewing
24+
//
25+
// Open 使用 w3m 浏览器在控制台中显示来自 URL 的网页内容
26+
// 使用 -dump 选项执行 w3m 以将网页渲染为文本输出
27+
// 记录渲染的网页内容以供基于控制台的网页查看
1228
func Open(command *osexec.OsCommand, link string) {
1329
output := rese.V1(command.Exec("w3m", "-dump", link))
1430
zaplog.SUG.Debug("[page]:", "\n", string(output), "\n", "----")
1531
}
1632

33+
// Show renders HTML content directly using w3m browser via stdin input
34+
// Executes w3m with HTML input to display content as console text output
35+
// Processes HTML strings and displays formatted text results
36+
//
37+
// Show 通过 stdin 输入使用 w3m 浏览器直接渲染 HTML 内容
38+
// 使用 HTML 输入执行 w3m 以将内容显示为控制台文本输出
39+
// 处理 HTML 字符串并显示格式化的文本结果
1740
func Show(command *osexec.OsCommand, page string) {
1841
output := rese.V1(command.ExecWith("w3m", []string{"-T", "text/html", "-dump"}, func(command *exec.Cmd) {
1942
command.Stdin = strings.NewReader(page)

w3mopenpage/w3m_open_page_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package w3mopenpage_test
22

33
import (
4+
"os/exec"
45
"testing"
56

67
"github.com/brianvoe/gofakeit/v7"
@@ -12,6 +13,11 @@ import (
1213
)
1314

1415
func TestShow(t *testing.T) {
16+
path, err := exec.LookPath("w3m")
17+
if err != nil {
18+
t.Skip("w3m is not available on this system, skipping test case")
19+
}
20+
t.Log("w3m path:", path)
1521
w3mopenpage.Show(osexec.NewOsCommand().WithDebug(), utils.NewPage("标题1", "内容1"))
1622
}
1723

@@ -29,6 +35,11 @@ func newAccount() *Account {
2935
}
3036

3137
func TestShowPage(t *testing.T) {
38+
path, err := exec.LookPath("w3m")
39+
if err != nil {
40+
t.Skip("w3m is not available on this system, skipping test case")
41+
}
42+
t.Log("w3m path:", path)
3243
w3mopenpage.Show(osexec.NewOsCommand().WithDebug(), utils.NewPage("accounts", slice2table.NewTable([]*Account{
3344
newAccount(),
3445
newAccount(),

w3mopenpage/w3m_show_page.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,74 @@ import (
66
"github.com/yyle88/osexec"
77
)
88

9+
// W3mShowPage manages w3m browser automation with webpage output control
10+
// Provides coordinated webpage generation and w3m opening with HTTP service
11+
// Handles temporary HTTP service and console browser operations
12+
//
13+
// W3mShowPage 管理 w3m 浏览器自动化与网页输出控制
14+
// 提供协调的网页生成和 w3m 打开及 HTTP 服务
15+
// 处理临时 HTTP 服务和控制台浏览器操作
916
type W3mShowPage struct {
1017
service *gintestpage.Service
1118
command *osexec.OsCommand
1219
}
1320

21+
// NewW3mShowPage creates w3m automation instance with custom service and command
22+
// Initializes console browser automation using provided HTTP service and OS command configuration
23+
// Returns configured w3m automation handler for coordinated console browser operations
24+
//
25+
// NewW3mShowPage 使用自定义服务和命令创建 w3m 自动化实例
26+
// 使用提供的 HTTP 服务和操作系统命令配置初始化控制台浏览器自动化
27+
// 返回配置的 w3m 自动化处理器以实现协调的控制台浏览器操作
1428
func NewW3mShowPage(service *gintestpage.Service, command *osexec.OsCommand) *W3mShowPage {
1529
return &W3mShowPage{
1630
service: service,
1731
command: command,
1832
}
1933
}
2034

35+
// NewW3mDrawPage creates w3m automation with default configuration for development
36+
// Initializes console browser automation using default HTTP test service and debug command setup
37+
// Returns ready-to-use w3m automation handler with optimal development settings
38+
//
39+
// NewW3mDrawPage 使用开发的默认配置创建 w3m 自动化
40+
// 使用默认 HTTP 测试服务和调试命令设置初始化控制台浏览器自动化
41+
// 返回具有优化开发设置的即用 w3m 自动化处理器
2142
func NewW3mDrawPage() *W3mShowPage {
2243
return NewW3mShowPage(gintestpage.NewService(), osexec.NewOsCommand().WithDebug())
2344
}
2445

46+
// Close terminates w3m automation and shuts down HTTP service
47+
// Stops the temporary HTTP test service used for webpage hosting
48+
// Cleans up resources used by w3m automation instance
49+
//
50+
// Close 终止 w3m 自动化并关闭 HTTP 服务
51+
// 停止用于网页托管的临时 HTTP 测试服务
52+
// 清理 w3m 自动化实例使用的资源
2553
func (op *W3mShowPage) Close() {
2654
op.service.Close()
2755
}
2856

57+
// Show displays single HTML page content using w3m browser in console
58+
// Creates temporary URL for HTML content and opens it via w3m for console viewing
59+
// Generates unique path and serves page through HTTP service
60+
//
61+
// Show 使用 w3m 浏览器在控制台中显示单个 HTML 页面内容
62+
// 为 HTML 内容创建临时 URL 并通过 w3m 打开以供控制台查看
63+
// 生成唯一路径并通过 HTTP 服务提供页面
2964
func (op *W3mShowPage) Show(page string) {
3065
path := uuid.New().String()
3166
link := op.service.SetPage(path, []byte(page))
3267
Open(op.command, link)
3368
}
3469

70+
// ShowPages displays multiple HTML pages sequentially using w3m browser
71+
// Creates temporary URLs for each HTML page and opens them via w3m for console viewing
72+
// Processes pages in order with unique paths for each page
73+
//
74+
// ShowPages 使用 w3m 浏览器依序显示多个 HTML 页面
75+
// 为每个 HTML 页面创建临时 URL 并通过 w3m 打开以供控制台查看
76+
// 为每个页面使用唯一路径按顺序处理页面
3577
func (op *W3mShowPage) ShowPages(pages ...string) {
3678
for _, page := range pages {
3779
path := uuid.New().String()

w3mopenpage/w3m_show_page_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package w3mopenpage_test
22

33
import (
4+
"os/exec"
45
"testing"
56

67
"github.com/brianvoe/gofakeit/v7"
@@ -21,6 +22,11 @@ func TestMain(m *testing.M) {
2122
}
2223

2324
func TestW3mShowPage_Show(t *testing.T) {
25+
path, err := exec.LookPath("w3m")
26+
if err != nil {
27+
t.Skip("w3m is not available on this system, skipping test case")
28+
}
29+
t.Log("w3m path:", path)
2430
caseDrawPage.Show(utils.NewPage("标题1", "内容1"))
2531
}
2632

@@ -37,6 +43,11 @@ func newEmployee() *Employee {
3743
}
3844

3945
func TestW3mShowPage_ShowPage(t *testing.T) {
46+
path, err := exec.LookPath("w3m")
47+
if err != nil {
48+
t.Skip("w3m is not available on this system, skipping test case")
49+
}
50+
t.Log("w3m path:", path)
4051
caseDrawPage.Show(utils.NewPage("employees", slice2table.NewTable([]*Employee{
4152
newEmployee(),
4253
newEmployee(),

0 commit comments

Comments
 (0)