From 62099d325d6ca31b915b95bfe115d00d31f8a0a6 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Mon, 25 Mar 2024 22:37:30 +0000 Subject: [PATCH] build: define new unified build interfaces. --- pkg/prebuild/cfg/core.go | 47 +++++++++++++++++++++++++ pkg/prebuild/cfg/core_test.go | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 pkg/prebuild/cfg/core.go create mode 100644 pkg/prebuild/cfg/core_test.go diff --git a/pkg/prebuild/cfg/core.go b/pkg/prebuild/cfg/core.go new file mode 100644 index 00000000..692f130a --- /dev/null +++ b/pkg/prebuild/cfg/core.go @@ -0,0 +1,47 @@ +// apparmor.d - Full set of apparmor profiles +// Copyright (C) 2021-2024 Alexandre Pujol +// SPDX-License-Identifier: GPL-2.0-only + +package cfg + +import "fmt" + +type BaseInterface interface { + Message() string + Name() string + Usage() string +} + +type Base struct { + Msg string + Keyword string + Help string +} + +func (b Base) Name() string { + return b.Keyword +} + +func (b Base) Usage() string { + return b.Help +} + +func (b Base) Message() string { + return b.Msg +} + +func Help[T BaseInterface](name string, tasks map[string]T) string { + res := fmt.Sprintf("%s tasks:\n", name) + for _, t := range tasks { + res += fmt.Sprintf(" %s - %s\n", t.Name(), t.Message()) + } + return res +} + +func Usage[T BaseInterface](name string, tasks map[string]T) string { + res := fmt.Sprintf("%s\n", name) + for _, t := range tasks { + res += fmt.Sprintf(" %s\n", t.Usage()) + } + return res +} diff --git a/pkg/prebuild/cfg/core_test.go b/pkg/prebuild/cfg/core_test.go new file mode 100644 index 00000000..ff76f946 --- /dev/null +++ b/pkg/prebuild/cfg/core_test.go @@ -0,0 +1,64 @@ +// apparmor.d - Full set of apparmor profiles +// Copyright (C) 2021-2024 Alexandre Pujol +// SPDX-License-Identifier: GPL-2.0-only + +package cfg + +import ( + "strings" + "testing" +) + +func TestBase_Helpers(t *testing.T) { + tests := []struct { + name string + b Base + want string + }{ + { + name: "base", + b: Base{Keyword: "test", Help: "test", Msg: "test"}, + want: "test", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.b.Name(); got != tt.want { + t.Errorf("Base.Name() = %v, want %v", got, tt.want) + } + if got := tt.b.Usage(); got != tt.want { + t.Errorf("Base.Usage() = %v, want %v", got, tt.want) + } + if got := tt.b.Message(); got != tt.want { + t.Errorf("Base.Message() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestHelp(t *testing.T) { + tests := []struct { + name string + tasks map[string]Base + want string + }{ + { + name: "one", + tasks: map[string]Base{ + "one": {Keyword: "one", Help: "one", Msg: "one"}, + "two": {Keyword: "two", Help: "two", Msg: "two"}, + }, + want: `one`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Help(tt.name, tt.tasks); !strings.Contains(got, tt.want) { + t.Errorf("Help() = %v, want %v", got, tt.want) + } + if got := Usage(tt.name, tt.tasks); !strings.Contains(got, tt.want) { + t.Errorf("Usage() = %v, want %v", got, tt.want) + } + }) + } +}