mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 18:10:21 +00:00
* test(cli): enhance unit test coverage for theme and color config This commit introduces a comprehensive suite of unit tests for the theme and color configuration functions in `references/cli/top/config`. Key changes include: - Refactored existing tests in `color_test.go` to use table-driven sub-tests for improved clarity and maintainability. - Added new test functions to validate color parsing, hex color detection, and default theme creation. - Implemented tests for theme file lifecycle management, including creation and loading logic. These additions significantly increase the test coverage and ensure the robustness and correctness of the CLI's theme and color functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(cli): refactor and enhance tests for top view models and utils This commit improves the unit test suite for the CLI's top view functionality by refactoring existing tests and adding new ones to increase coverage. Key changes include: - In `application_test.go`, `TestApplicationList_ToTableBody` is refactored to be a table-driven test, and new tests are added for `serviceNum`, `workflowMode`, and `workflowStepNum` helpers. - In `time_test.go`, `TestTimeFormat` is refactored into a table-driven test for better structure and readability. These changes align the tests with best practices and improve the overall robustness of the CLI top view's data presentation logic. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(cuegen): enhance unit test coverage for CUE generation packages This commit introduces a comprehensive suite of unit tests and refactors existing tests for the CUE generation packages located in `references/cuegen`. Key changes include: - Refactored existing tests in `generator_test.go` and `provider_test.go` to use table-driven sub-tests, improving clarity, maintainability, and coverage of error conditions. - Added new test functions to `convert_test.go` to validate helper functions for comment generation, type support, and enum field handling. - Added new tests in `provider_test.go` to cover provider extraction, declaration modification, and panic recovery logic. These changes significantly increase the test coverage for the `cuegen` libraries, ensuring the correctness and robustness of the CUE code generation functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(docgen): add comprehensive unit tests for doc generation This commit introduces a comprehensive suite of unit tests for the documentation generation package located in `references/docgen`. Key changes include: - Added new test files (`console_test.go`, `convert_test.go`, `openapi_test.go`) to cover the core functions for parsing and generating documentation for CUE, Terraform, and OpenAPI schemas. - Refactored and enhanced `i18n_test.go` to use sub-tests, resolve race conditions, and improve coverage for fallback logic and error handling. - Ensured all new and existing tests follow best practices, using table-driven tests for clarity and maintainability. This effort significantly increases the test coverage for the `docgen` package, improving the reliability and robustness of the documentation generation features. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test: improve test reliability and conventions This commit introduces several improvements to the test suite to enhance reliability and adhere to best practices. - **Fix flaky test in `docgen/openapi_test.go`**: The test for `GenerateConsoleDocument` was flaky because it performed an exact string match on table output generated from a map. Since map iteration order is not guaranteed, this could cause spurious failures. The test is now order-insensitive, comparing sorted sets of lines instead. - **Improve assertions in `docgen/console_test.go`**: - Removes an unnecessary `test.EquateErrors()` option, which is not needed for simple string comparisons. - Corrects the `cmp.Diff` argument order to the standard `(want, got)` convention for clearer failure messages. - Fixes a typo in an error message. - **Standardize assertions in `cli/top/config/color_test.go`**: Swaps `assert.Equal` arguments to the standard `(expected, actual)` convention. - **Clean up `cuegen/generators/provider/provider_test.go`**: Removes a redundant error check. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> --------- Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
188 lines
4.8 KiB
Go
188 lines
4.8 KiB
Go
/*
|
|
Copyright 2021 The KubeVela Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package docgen
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/require"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
|
"github.com/oam-dev/kubevela/apis/types"
|
|
)
|
|
|
|
func TestGenerateCUETemplateProperties(t *testing.T) {
|
|
// Read componentDef for a valid capability
|
|
componentDefYAML, err := os.ReadFile("testdata/componentDef.yaml")
|
|
require.NoError(t, err)
|
|
var componentDef v1beta1.ComponentDefinition
|
|
err = yaml.Unmarshal(componentDefYAML, &componentDef)
|
|
require.NoError(t, err)
|
|
|
|
// Define a struct to unmarshal the raw extension
|
|
type extensionSpec struct {
|
|
Template string `json:"template"`
|
|
}
|
|
var extSpec extensionSpec
|
|
err = yaml.Unmarshal(componentDef.Spec.Extension.Raw, &extSpec)
|
|
require.NoError(t, err)
|
|
|
|
// Define test cases
|
|
testCases := []struct {
|
|
name string
|
|
capability *types.Capability
|
|
expectedTables int
|
|
expectErr bool
|
|
}{
|
|
{
|
|
name: "valid component definition",
|
|
capability: &types.Capability{
|
|
Name: "test-component",
|
|
CueTemplate: extSpec.Template,
|
|
},
|
|
expectedTables: 2,
|
|
expectErr: false,
|
|
},
|
|
{
|
|
name: "invalid cue template",
|
|
capability: &types.Capability{
|
|
Name: "invalid-cue",
|
|
CueTemplate: `parameter: { image: }`,
|
|
},
|
|
expectErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ref := &ConsoleReference{}
|
|
doc, console, err := ref.GenerateCUETemplateProperties(tc.capability)
|
|
|
|
if tc.expectErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
require.NotNil(t, doc)
|
|
require.Len(t, console, tc.expectedTables)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGenerateTerraformCapabilityProperties(t *testing.T) {
|
|
ref := &ConsoleReference{}
|
|
type args struct {
|
|
cap types.Capability
|
|
}
|
|
|
|
type want struct {
|
|
tableName1 string
|
|
tableName2 string
|
|
errMsg string
|
|
}
|
|
testcases := map[string]struct {
|
|
args args
|
|
want want
|
|
}{
|
|
"normal": {
|
|
args: args{
|
|
cap: types.Capability{
|
|
TerraformConfiguration: `
|
|
resource "alicloud_oss_bucket" "bucket-acl" {
|
|
bucket = var.bucket
|
|
acl = var.acl
|
|
}
|
|
|
|
output "BUCKET_NAME" {
|
|
value = "${alicloud_oss_bucket.bucket-acl.bucket}.${alicloud_oss_bucket.bucket-acl.extranet_endpoint}"
|
|
}
|
|
|
|
variable "bucket" {
|
|
description = "OSS bucket name"
|
|
default = "vela-website"
|
|
type = string
|
|
}
|
|
|
|
variable "acl" {
|
|
description = "OSS bucket ACL, supported 'private', 'public-read', 'public-read-write'"
|
|
default = "private"
|
|
type = string
|
|
}
|
|
`,
|
|
},
|
|
},
|
|
want: want{
|
|
errMsg: "",
|
|
tableName1: "",
|
|
tableName2: "#### writeConnectionSecretToRef",
|
|
},
|
|
},
|
|
"configuration is in git remote": {
|
|
args: args{
|
|
cap: types.Capability{
|
|
Name: "ecs",
|
|
TerraformConfiguration: "https://github.com/wonderflow/terraform-alicloud-ecs-instance.git",
|
|
ConfigurationType: "remote",
|
|
},
|
|
},
|
|
want: want{
|
|
errMsg: "",
|
|
tableName1: "",
|
|
tableName2: "#### writeConnectionSecretToRef",
|
|
},
|
|
},
|
|
"configuration is not valid": {
|
|
args: args{
|
|
cap: types.Capability{
|
|
TerraformConfiguration: `abc`,
|
|
},
|
|
},
|
|
want: want{
|
|
errMsg: "failed to generate capability properties: :1,1-4: Argument or block definition required; An " +
|
|
"argument or block definition is required here. To set an argument, use the equals sign \"=\" to " +
|
|
"introduce the argument value.",
|
|
},
|
|
},
|
|
}
|
|
for name, tc := range testcases {
|
|
t.Run(name, func(t *testing.T) {
|
|
consoleRef, err := ref.GenerateTerraformCapabilityProperties(tc.args.cap)
|
|
var errMsg string
|
|
if err != nil {
|
|
errMsg = err.Error()
|
|
if diff := cmp.Diff(tc.want.errMsg, errMsg); diff != "" {
|
|
t.Errorf("\n%s\nGenerateTerraformCapabilityProperties(...): -want error, +got error:\n%s\n", name, diff)
|
|
}
|
|
} else {
|
|
if diff := cmp.Diff(2, len(consoleRef)); diff != "" {
|
|
t.Errorf("\n%s\nGenerateTerraformCapabilityProperties(...): -want, +got:\n%s\n", name, diff)
|
|
}
|
|
if diff := cmp.Diff(tc.want.tableName1, consoleRef[0].TableName); diff != "" {
|
|
t.Errorf("\n%s\nGenerateTerraformCapabilityProperties(...): -want, +got:\n%s\n", name, diff)
|
|
}
|
|
if diff := cmp.Diff(tc.want.tableName2, consoleRef[1].TableName); diff != "" {
|
|
t.Errorf("\n%s\nGenerateTerraformCapabilityProperties(...): -want, +got:\n%s\n", name, diff)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|