mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
refactor: drop single-use abstractions, loosen varnamelen (#680)
log.Base had one implementation, the chromium file specs took a dir they never read, and emit was written three times; varnamelen now lets db, fd, and n stay short as Go readers expect.
This commit is contained in:
1 parent
02d2e2bda6
commit
2c3ac7f1d2
38 files changed
+109
-153
No files matched your search
+1
-1
@@ -163,7 +163,7 @@ linters:
|
||||
varnamelen:
|
||||
# 1 is the strictest the linter allows: it only spares a name used on the line after its declaration.
|
||||
max-distance: 1
|
||||
ignore-names: [i, j, ok, id, iv]
|
||||
ignore-names: [i, j, ok, id, iv, db, fd, n]
|
||||
ignore-decls:
|
||||
- t *testing.T
|
||||
- b *testing.B
|
||||
|
||||
@@ -59,7 +59,7 @@ make payload-clean # rm internal/abe/payload/*.bin
|
||||
- **C payload**: all sources in `internal/abe/native/` are first-party (no vendored C). See RFC-016 §9.2 for why Stephen Fewer's reflective loader was rejected.
|
||||
- **Error handling**: `fmt.Errorf("context: %w", err)` for wrapping, never `_ =` to ignore errors
|
||||
- **Logging**: `log.Debugf` for record-level diagnostics, `log.Infof` for user-facing progress/status, `log.Warnf` for unexpected conditions. Extract methods should return errors, not log them.
|
||||
- **Naming**: follow Go conventions and the settled names in `rfcs/004-naming-decisions.md` — `browser.Browser` not `BrowserConfig`, `Extract` not `BrowsingData`; spell names out (`extractors`, `snapshotDir`, `profile`), leaving only `i`/`j`, `err`, `ctx`, `ok`, `t`/`tb`, `id`, `iv`, and one-letter receivers short (`varnamelen` enforces it)
|
||||
- **Naming**: follow Go conventions and the settled names in `rfcs/004-naming-decisions.md` — `browser.Browser` not `BrowserConfig`, `Extract` not `BrowsingData`; spell names out (`extractors`, `snapshotDir`, `profile`), leaving only `i`/`j`, `err`, `ctx`, `ok`, `t`/`tb`, `id`, `iv`, `db`, `fd`, `n`, and one-letter receivers short (`varnamelen` enforces it)
|
||||
- **Comments**: none by default; when one is needed, one line of at most 120 characters that says why, never what the code does (up to 3 lines only when the reason cannot fit); a comment on a declaration starts with its name (`// AssertJSONFallback checks …`)
|
||||
- **Tests**: `internal/browser/browsertest` builds real fixture files (SQLite, JSON, LevelDB, plist) into a `t.TempDir()` — never a live browser, never a database mock; goldens live in each package's `testdata/` and `HBD_UPDATE_GOLDEN=1 go test ./...` regenerates them
|
||||
- **Architecture**: see `rfcs/README.md` — design RFCs 000–011, format and technique RFCs 013–021
|
||||
|
||||
@@ -50,7 +50,7 @@ func archiveFiles(plan Plan, dirs []browser.UserDataDir) []snapshot.File {
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
for _, profile := range dir.Profiles {
|
||||
for _, fileSpec := range chromium.ArchiveFileSpecs(dir) {
|
||||
for _, fileSpec := range chromium.ArchiveFileSpecs() {
|
||||
if file, ok, err := firstOnDisk(dir, profile, fileSpec); err == nil && ok {
|
||||
add(file)
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func retrieveKeys(ctx context.Context, dir browser.UserDataDir, keys masterKeyRe
|
||||
}
|
||||
defer closeSnapshot(snapshotDir)
|
||||
var files []snapshot.File
|
||||
for _, fileSpec := range chromium.KeyFileSpecs(dir) {
|
||||
for _, fileSpec := range chromium.KeyFileSpecs() {
|
||||
if file, ok, err := firstOnDisk(dir, browser.Profile{}, fileSpec); err == nil && ok {
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
@@ -172,12 +172,12 @@ func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterk
|
||||
}
|
||||
|
||||
// KeyFileSpecs are the files KeyLocator reads from a snapshot.
|
||||
func KeyFileSpecs(_ browser.UserDataDir) []browser.FileSpec {
|
||||
func KeyFileSpecs() []browser.FileSpec {
|
||||
return []browser.FileSpec{localState}
|
||||
}
|
||||
|
||||
// ArchiveFileSpecs add each profile's marker, by which restore rediscovers the profiles, and Local State as v1.1.0 did.
|
||||
func ArchiveFileSpecs(_ browser.UserDataDir) []browser.FileSpec {
|
||||
func ArchiveFileSpecs() []browser.FileSpec {
|
||||
fileSpecs := []browser.FileSpec{localState}
|
||||
for _, marker := range profileMarkers {
|
||||
fileSpecs = append(fileSpecs, browser.FileSpec{Paths: []string{marker}, Optional: true})
|
||||
|
||||
@@ -329,5 +329,5 @@ func TestArchiveFileSpecs(t *testing.T) {
|
||||
localState,
|
||||
{Paths: []string{"Preferences"}, Optional: true},
|
||||
{Paths: []string{"Preferences_02"}, Optional: true},
|
||||
}, ArchiveFileSpecs(browser.UserDataDir{}))
|
||||
}, ArchiveFileSpecs())
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func extractBookmarks(path string, out browser.Collector) error {
|
||||
walkBookmarks(value, "", &bookmarks)
|
||||
return true
|
||||
})
|
||||
emit(out, bookmarks)
|
||||
browser.AddAll(out, bookmarks)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ func extractCookies(masterKeys masterkey.Set, path string, out browser.Collector
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, cookies)
|
||||
browser.AddAll(out, cookies)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func extractCreditCards(masterKeys masterkey.Set, path string, out browser.Colle
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, cards)
|
||||
browser.AddAll(out, cards)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -110,6 +110,6 @@ func extractYandexCreditCards(masterKeys masterkey.Set, path string, out browser
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, cards)
|
||||
browser.AddAll(out, cards)
|
||||
return nil
|
||||
}
|
||||
@@ -32,6 +32,6 @@ func extractDownloads(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, downloads)
|
||||
browser.AddAll(out, downloads)
|
||||
return nil
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func extractExtensionsWithKeys(path string, keys []string, out browser.Collector
|
||||
return true
|
||||
})
|
||||
|
||||
emit(out, extensions)
|
||||
browser.AddAll(out, extensions)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,6 @@ func extractHistories(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, histories)
|
||||
browser.AddAll(out, histories)
|
||||
return nil
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func extractPasswordsWithQuery(masterKeys masterkey.Set, path string, store reco
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, logins)
|
||||
browser.AddAll(out, logins)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -85,6 +85,6 @@ func extractYandexPasswords(masterKeys masterkey.Set, path string, out browser.C
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, logins)
|
||||
browser.AddAll(out, logins)
|
||||
return nil
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func extractLocalStorage(path string, out browser.Collector) error {
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
emit(out, entries)
|
||||
browser.AddAll(out, entries)
|
||||
return cursor.Error()
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ func extractSessionStorage(path string, out browser.Collector) error {
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
emit(out, entries)
|
||||
browser.AddAll(out, entries)
|
||||
return mapCursor.Error()
|
||||
}
|
||||
|
||||
|
||||
@@ -109,13 +109,3 @@ func extractPasswordStores(ctx context.Context, profileSnapshot browser.ProfileS
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place.
|
||||
func emit[T any, P interface {
|
||||
*T
|
||||
record.Record
|
||||
}](out browser.Collector, rows []T) {
|
||||
for i := range rows {
|
||||
out.Add(P(&rows[i]))
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,16 @@ type Collector interface {
|
||||
Fail(err error)
|
||||
}
|
||||
|
||||
// AddAll hands rows to out by pointer, because out sets each record's Browser and Profile in place.
|
||||
func AddAll[T any, P interface {
|
||||
*T
|
||||
record.Record
|
||||
}](out Collector, rows []T) {
|
||||
for i := range rows {
|
||||
out.Add(P(&rows[i]))
|
||||
}
|
||||
}
|
||||
|
||||
type ProfileSnapshot struct {
|
||||
Profile Profile
|
||||
snapshot snapshot.Snapshot
|
||||
|
||||
@@ -36,7 +36,7 @@ func extractBookmarks(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, bookmarks)
|
||||
browser.AddAll(out, bookmarks)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,6 @@ func extractCookies(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, cookies)
|
||||
browser.AddAll(out, cookies)
|
||||
return nil
|
||||
}
|
||||
@@ -46,6 +46,6 @@ func extractDownloads(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, downloads)
|
||||
browser.AddAll(out, downloads)
|
||||
return nil
|
||||
}
|
||||
@@ -32,6 +32,6 @@ func extractExtensions(path string, out browser.Collector) error {
|
||||
Enabled: value.Get("active").Bool(),
|
||||
})
|
||||
}
|
||||
emit(out, extensions)
|
||||
browser.AddAll(out, extensions)
|
||||
return nil
|
||||
}
|
||||
@@ -31,6 +31,6 @@ func extractHistories(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, histories)
|
||||
browser.AddAll(out, histories)
|
||||
return nil
|
||||
}
|
||||
@@ -60,6 +60,6 @@ func extractPasswords(masterKey func() []byte, path string, out browser.Collecto
|
||||
Store: record.StoreLocal,
|
||||
})
|
||||
}
|
||||
emit(out, logins)
|
||||
browser.AddAll(out, logins)
|
||||
return nil
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func extractLocalStorage(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, entries)
|
||||
browser.AddAll(out, entries)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -161,13 +160,3 @@ func profileKey(profileSnapshot browser.ProfileSnapshot, loginsPath string) ([]b
|
||||
}
|
||||
return retrieveMasterKey(key4Path, loginsPath)
|
||||
}
|
||||
|
||||
// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place.
|
||||
func emit[T any, P interface {
|
||||
*T
|
||||
record.Record
|
||||
}](out browser.Collector, rows []T) {
|
||||
for i := range rows {
|
||||
out.Add(P(&rows[i]))
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func extractBookmarks(path string, out browser.Collector) error {
|
||||
|
||||
var bookmarks []record.Bookmark
|
||||
walkBookmarks(root.Children, "", &bookmarks)
|
||||
emit(out, bookmarks)
|
||||
browser.AddAll(out, bookmarks)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ func extractCookies(path string, out browser.Collector) error {
|
||||
})
|
||||
}
|
||||
}
|
||||
emit(out, cookies)
|
||||
browser.AddAll(out, cookies)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func extractDownloads(path, ownerUUID string, out browser.Collector) error {
|
||||
TotalBytes: entry.TotalBytes,
|
||||
})
|
||||
}
|
||||
emit(out, downloads)
|
||||
browser.AddAll(out, downloads)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ func extractExtensions(paths []string, out browser.Collector) error {
|
||||
Enabled: entry.enabled,
|
||||
})
|
||||
}
|
||||
emit(out, extensions)
|
||||
browser.AddAll(out, extensions)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,6 @@ func extractHistories(path string, out browser.Collector) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
emit(out, histories)
|
||||
browser.AddAll(out, histories)
|
||||
return nil
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func extractPasswords(ctx context.Context, loginKeychain LoginKeychain, out brow
|
||||
Store: record.StoreLocal,
|
||||
})
|
||||
}
|
||||
emit(out, logins)
|
||||
browser.AddAll(out, logins)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ func extractLocalStorage(root string, out browser.Collector) error {
|
||||
})
|
||||
}
|
||||
}
|
||||
emit(out, entries)
|
||||
browser.AddAll(out, entries)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
)
|
||||
|
||||
// Paths below are relative to the user data dir, ~/Library.
|
||||
@@ -172,13 +171,3 @@ func extensionsFrom(library func(string) string) browser.Extractor {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place.
|
||||
func emit[T any, P interface {
|
||||
*T
|
||||
record.Record
|
||||
}](out browser.Collector, rows []T) {
|
||||
for i := range rows {
|
||||
out.Add(P(&rows[i]))
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -1,8 +1,11 @@
|
||||
package log
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var defaultLogger = NewLogger(nil)
|
||||
var defaultLogger = newLogger(os.Stderr)
|
||||
|
||||
func SetVerbose() {
|
||||
defaultLogger.SetLevel(DebugLevel)
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestSetVerbose(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetLevelInvalidPanics(t *testing.T) {
|
||||
logger := NewLogger(newBase(io.Discard))
|
||||
assert.Panics(t, func() { logger.SetLevel(DebugLevel - 1) })
|
||||
assert.Panics(t, func() { logger.SetLevel(WarnLevel + 1) })
|
||||
testLogger := newLogger(io.Discard)
|
||||
assert.Panics(t, func() { testLogger.SetLevel(DebugLevel - 1) })
|
||||
assert.Panics(t, func() { testLogger.SetLevel(WarnLevel + 1) })
|
||||
}
|
||||
+26
-51
@@ -3,7 +3,6 @@ package log
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -11,75 +10,43 @@ import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func NewLogger(base Base) *Logger {
|
||||
if base == nil {
|
||||
base = newBase(os.Stderr)
|
||||
}
|
||||
return &Logger{base: base, minLevel: InfoLevel}
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
base Base
|
||||
|
||||
type logger struct {
|
||||
out io.Writer
|
||||
mutex sync.Mutex
|
||||
minLevel Level
|
||||
}
|
||||
|
||||
func (l *Logger) canLogAt(level Level) bool {
|
||||
func newLogger(out io.Writer) *logger {
|
||||
return &logger{out: out, minLevel: InfoLevel}
|
||||
}
|
||||
|
||||
func (l *logger) canLogAt(level Level) bool {
|
||||
return level >= Level(atomic.LoadInt32((*int32)(&l.minLevel)))
|
||||
}
|
||||
|
||||
func (l *Logger) SetLevel(level Level) {
|
||||
func (l *logger) SetLevel(level Level) {
|
||||
if level < DebugLevel || level > WarnLevel {
|
||||
panic("log: invalid log level")
|
||||
}
|
||||
atomic.StoreInt32((*int32)(&l.minLevel), int32(level))
|
||||
}
|
||||
|
||||
// baseCallerSkip skips the frames between runtime.Caller and the caller: base.Log, logMsg, and Debugf/Infof/Warnf.
|
||||
const baseCallerSkip = 3
|
||||
|
||||
func (l *Logger) logMsg(level Level, message string) {
|
||||
if !l.canLogAt(level) {
|
||||
return
|
||||
}
|
||||
l.base.Log(baseCallerSkip, level, message)
|
||||
}
|
||||
|
||||
func (l *Logger) Debugf(format string, args ...any) {
|
||||
l.logMsg(DebugLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (l *Logger) Infof(format string, args ...any) {
|
||||
l.logMsg(InfoLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (l *Logger) Warnf(format string, args ...any) {
|
||||
l.logMsg(WarnLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
type Base interface {
|
||||
Log(callerSkip int, lvl Level, msg string)
|
||||
}
|
||||
|
||||
type baseLogger struct {
|
||||
out io.Writer
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func newBase(out io.Writer) *baseLogger {
|
||||
return &baseLogger{out: out}
|
||||
}
|
||||
// callerSkip skips the frames between runtime.Caller and the caller: logMsg, and Debugf/Infof/Warnf.
|
||||
const callerSkip = 2
|
||||
|
||||
// continuation lines a message's later lines up under its first, past the "[DBG] " label.
|
||||
const continuation = " "
|
||||
|
||||
func (l *baseLogger) Log(callerSkip int, level Level, message string) {
|
||||
func (l *logger) logMsg(level Level, message string) {
|
||||
if !l.canLogAt(level) {
|
||||
return
|
||||
}
|
||||
message = strings.TrimRight(message, "\n")
|
||||
if strings.Contains(message, "\n") {
|
||||
message = strings.ReplaceAll(message, "\n", "\n"+continuation)
|
||||
}
|
||||
|
||||
label := l.formatLabel(level)
|
||||
label := "[" + level.String() + "]"
|
||||
var line string
|
||||
if level == DebugLevel {
|
||||
_, file, lineNumber, ok := runtime.Caller(callerSkip)
|
||||
@@ -99,6 +66,14 @@ func (l *baseLogger) Log(callerSkip int, level Level, message string) {
|
||||
l.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (l *baseLogger) formatLabel(level Level) string {
|
||||
return "[" + level.String() + "]"
|
||||
func (l *logger) Debugf(format string, args ...any) {
|
||||
l.logMsg(DebugLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (l *logger) Infof(format string, args ...any) {
|
||||
l.logMsg(InfoLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (l *logger) Warnf(format string, args ...any) {
|
||||
l.logMsg(WarnLevel, fmt.Sprintf(format, args...))
|
||||
}
|
||||
+31
-31
@@ -37,9 +37,9 @@ func TestLoggerDebug(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[DBG\] \w+\.go:\d+: %s\n$`, testCase.message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.SetLevel(DebugLevel)
|
||||
logger.Debugf("%s", message)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.SetLevel(DebugLevel)
|
||||
testLogger.Debugf("%s", message)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -54,8 +54,8 @@ func TestLoggerInfo(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[INF\] %s\n$`, testCase.message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Infof("%s", message)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Infof("%s", message)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -70,8 +70,8 @@ func TestLoggerWarn(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[WRN\] %s\n$`, testCase.message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Warnf("%s", message)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Warnf("%s", message)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -112,9 +112,9 @@ func TestLoggerDebugf(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[DBG\] \w+\.go:\d+: %s\n$`, message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.SetLevel(DebugLevel)
|
||||
logger.Debugf(testCase.format, testCase.args...)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.SetLevel(DebugLevel)
|
||||
testLogger.Debugf(testCase.format, testCase.args...)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -129,8 +129,8 @@ func TestLoggerInfof(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[INF\] %s\n$`, message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Infof(testCase.format, testCase.args...)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Infof(testCase.format, testCase.args...)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -145,8 +145,8 @@ func TestLoggerWarnf(t *testing.T) {
|
||||
testCase.wantedPattern = fmt.Sprintf(`^\[WRN\] %s\n$`, message)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Warnf(testCase.format, testCase.args...)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Warnf(testCase.format, testCase.args...)
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, testCase.wantedPattern, got)
|
||||
})
|
||||
@@ -158,21 +158,21 @@ func TestLoggerWithLowerLevels(t *testing.T) {
|
||||
ops := []struct {
|
||||
name string
|
||||
level Level
|
||||
logFunc func(*Logger)
|
||||
logFunc func(*logger)
|
||||
}{
|
||||
{"Debugf", DebugLevel, func(logger *Logger) { logger.Debugf("hello") }},
|
||||
{"Infof", InfoLevel, func(logger *Logger) { logger.Infof("hello") }},
|
||||
{"Warnf", WarnLevel, func(logger *Logger) { logger.Warnf("hello") }},
|
||||
{"Debugf", DebugLevel, func(testLogger *logger) { testLogger.Debugf("hello") }},
|
||||
{"Infof", InfoLevel, func(testLogger *logger) { testLogger.Infof("hello") }},
|
||||
{"Warnf", WarnLevel, func(testLogger *logger) { testLogger.Warnf("hello") }},
|
||||
}
|
||||
|
||||
for _, setLevel := range levels {
|
||||
for _, operation := range ops {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.SetLevel(setLevel)
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.SetLevel(setLevel)
|
||||
|
||||
expectedOutput := operation.level >= setLevel
|
||||
operation.logFunc(logger)
|
||||
operation.logFunc(testLogger)
|
||||
|
||||
output := buffer.String()
|
||||
if expectedOutput {
|
||||
@@ -186,35 +186,35 @@ func TestLoggerWithLowerLevels(t *testing.T) {
|
||||
|
||||
func TestDefaultLevelIsInfo(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
testLogger := newLogger(&buffer)
|
||||
|
||||
logger.Debugf("debug msg")
|
||||
testLogger.Debugf("debug msg")
|
||||
assert.Empty(t, buffer.String(), "Debug should be suppressed at default InfoLevel")
|
||||
|
||||
logger.Infof("info msg")
|
||||
testLogger.Infof("info msg")
|
||||
assert.Contains(t, buffer.String(), "info msg")
|
||||
}
|
||||
|
||||
func TestDebugIncludesFileLine(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.SetLevel(DebugLevel)
|
||||
logger.Debugf("test location")
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.SetLevel(DebugLevel)
|
||||
testLogger.Debugf("test location")
|
||||
got := buffer.String()
|
||||
assert.Regexp(t, `^\[DBG\] logger_test\.go:\d+: test location\n$`, got)
|
||||
}
|
||||
|
||||
func TestInfoHasLabel(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Infof("clean message")
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Infof("clean message")
|
||||
assert.Equal(t, "[INF] clean message\n", buffer.String())
|
||||
}
|
||||
|
||||
func TestMultilineMessageIndented(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
logger := NewLogger(newBase(&buffer))
|
||||
logger.Warnf("line1\nline2\nline3")
|
||||
testLogger := newLogger(&buffer)
|
||||
testLogger.Warnf("line1\nline2\nline3")
|
||||
got := buffer.String()
|
||||
assert.Equal(t, "[WRN] line1\n line2\n line3\n", got)
|
||||
}
|
||||
@@ -251,7 +251,7 @@ A category says what kind of data is meant; a record is one row of that data. Th
|
||||
|
||||
**Decision.** A variable, parameter, or field is named for what it holds: `for dataCategory, extractor := range defaultExtractors`, `snapshotDir, err := snapshot.Temp()`. Go's own guidance prefers short names in short scopes, but a reader of this code, human or agent, should not have to decode `c`, `e`, `ex`, or `d`, whose type is often not on the same line.
|
||||
|
||||
- **What stays short.** The idioms every Go reader knows: `i` and `j` as indexes, `err`, `ctx`, `ok`, `t` and `tb` in tests, the terms `id` and `iv`, and one-letter method receivers, whose type is on the same line.
|
||||
- **What stays short.** The idioms every Go reader knows: `i` and `j` as indexes, `err`, `ctx`, `ok`, `t` and `tb` in tests, the terms `id` and `iv`, `db` for a database handle, `fd` for a file descriptor, `n` for a count, and one-letter method receivers, whose type is on the same line.
|
||||
- **Package names.** A variable cannot take the name of its package — `browser`, `category`, `record`, `keychain` — without shadowing it, so it takes its role: `supportedBrowser`, `dataCategory`, `row`, `loginKeychain`.
|
||||
- **Enforcement.** `varnamelen` rejects a one- or two-letter name used beyond the next line; `max-distance: 1` is the strictest it allows. The Windows API and ABE packages are excluded until they get their own pass.
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ The Chromium base supplies two more, for the files no single category owns (belo
|
||||
```go
|
||||
// internal/browser/chromium only
|
||||
func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterkey.Locator, error)
|
||||
func ArchiveFileSpecs(dir browser.UserDataDir) []browser.FileSpec
|
||||
func ArchiveFileSpecs() []browser.FileSpec
|
||||
```
|
||||
|
||||
Safari's `Extractors` takes one more argument, the run's login keychain, which its password extractor captures. Only the default profile's map gets a password extractor, because the keychain belongs to the user, not to one profile ([RFC-006](006-key-retrieval-and-crypto.md)). `LoginKeychain` is the one-method interface Safari declares; `*keychain.Keychain` satisfies it.
|
||||
|
||||
Reference in new issue
Block a user