From e4ab3b2e65a78dc31f623a7f0cd8eff30dc4f8fe Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 03:40:06 +0300 Subject: [PATCH 01/11] chore: implement logger test --- pkl/logger_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 pkl/logger_test.go diff --git a/pkl/logger_test.go b/pkl/logger_test.go new file mode 100644 index 00000000..de493c87 --- /dev/null +++ b/pkl/logger_test.go @@ -0,0 +1,39 @@ +package pkl + +import ( + "io" + "testing" +) + +type writerMock struct { + err error + bytes int +} + +func (w writerMock) Write(_ []byte) (n int, err error) { + return w.bytes, w.err +} + +func TestLogger(t *testing.T) { + + tests := map[string]struct { + writerMock io.Writer + msg string + frameURI string + }{ + "should successfully log a message as trace and warn": { + writerMock: writerMock{err: nil, bytes: 20}, + msg: "test message", + frameURI: "test", + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + lgr := NewLogger(tc.writerMock) + lgr.Trace(tc.msg, tc.frameURI) + lgr.Warn(tc.msg, tc.frameURI) + }) + } +} From 52964540da5d8191b24af214a19eb3c269132a93 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 03:40:19 +0300 Subject: [PATCH 02/11] chore: enhance unmarshal tests coverage --- pkl/unmarshal_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkl/unmarshal_test.go b/pkl/unmarshal_test.go index 3bee36d5..f0295b41 100644 --- a/pkl/unmarshal_test.go +++ b/pkl/unmarshal_test.go @@ -19,6 +19,7 @@ package pkl_test import ( "bytes" _ "embed" + "fmt" "testing" "github.com/apple/pkl-go/pkl" @@ -483,3 +484,18 @@ func TestUnmarshal_Types_Pre_030(t *testing.T) { assert.Equal(t, types.Types{}, res) } + +func TestUnmarshal_NonPointerTypes(t *testing.T) { + var res types.Types + err := pkl.Unmarshal([]byte{}, res) + assert.NotNil(t, err) + assert.EqualError(t, fmt.Errorf("cannot unmarshal non-pointer. Got kind: struct"), err.Error()) +} + +func TestUnmarshal_Nil(t *testing.T) { + var s *string + err := pkl.Unmarshal([]byte{}, s) + assert.NotNil(t, err) + + assert.EqualError(t, fmt.Errorf("cannot unmarshal into nil"), err.Error()) +} From d8c9571664e4794f7d9476bb5a8194911d9e3fdc Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 03:53:53 +0300 Subject: [PATCH 03/11] chore: implement duration string and go duration tests --- pkl/values_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/pkl/values_test.go b/pkl/values_test.go index 19824521..f55dcd7a 100644 --- a/pkl/values_test.go +++ b/pkl/values_test.go @@ -17,7 +17,9 @@ package pkl import ( + "fmt" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -95,3 +97,78 @@ func TestDataSize_ConvertToUnit(t *testing.T) { }) } } + +func TestDuration_String(t *testing.T) { + tests := map[string]struct { + d Duration + want string + }{ + "should successfully return valid seconds string": { + d: Duration{ + Unit: Second, + }, + want: "s", + }, + "should successfully return valid milliseconds string": { + d: Duration{ + Unit: Millisecond, + }, + want: "ms", + }, + "should successfully return valid nanoseconds string": { + d: Duration{ + Unit: Nanosecond, + }, + want: "ns", + }, + "should successfully return valid microseconds string": { + d: Duration{ + Unit: Microsecond, + }, + want: "us", + }, + "should successfully return valid minute string": { + d: Duration{ + Unit: Minute, + }, + want: "min", + }, + "should successfully return valid hour string": { + d: Duration{ + Unit: Hour, + }, + want: "h", + }, + "should successfully return valid day string": { + d: Duration{ + Unit: Day, + }, + want: "d", + }, + "should return invalid string when provided unknown DurationUnit": { + d: Duration{ + Unit: DurationUnit(-1), + }, + want: "", + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + + assert.Equal(t, tc.want, fmt.Sprintf("%s", tc.d.Unit.String())) + }) + } +} + +func TestDuration_GoDuration(t *testing.T) { + t.Run("should successfully convert to GoDuration in seconds", func(t *testing.T) { + t.Parallel() + d := Duration{ + Value: 1, + Unit: Second, + } + assert.Equal(t, d.GoDuration(), 1*time.Second) + }) +} From d474caf45e22b0b5125c13d1797cd6852a16d278 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 04:08:34 +0300 Subject: [PATCH 04/11] chore: implement data size unmarshal binary --- pkl/values_test.go | 121 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 5 deletions(-) diff --git a/pkl/values_test.go b/pkl/values_test.go index f55dcd7a..089179cd 100644 --- a/pkl/values_test.go +++ b/pkl/values_test.go @@ -56,11 +56,39 @@ func TestDataSize_String(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { + t.Parallel() assert.Equal(t, test.expected, test.input.String()) }) } } +func TestDataSizeUnit_String(t *testing.T) { + tests := []struct { + name string + unit DataSizeUnit + want string + }{ + {name: "byte", unit: Bytes, want: "b"}, + {name: "kilobyte", unit: Kilobytes, want: "kb"}, + {name: "kibibyte", unit: Kibibytes, want: "kib"}, + {name: "megabyte", unit: Megabytes, want: "mb"}, + {name: "mebibyte", unit: Mebibytes, want: "mib"}, + {name: "gigabyte", unit: Gigabytes, want: "gb"}, + {name: "gibibyte", unit: Gibibytes, want: "gib"}, + {name: "terabyte", unit: Terabytes, want: "tb"}, + {name: "tebibyte", unit: Tebibytes, want: "tib"}, + {name: "petabyte", unit: Petabytes, want: "pb"}, + {name: "pebibyte", unit: Pebibytes, want: "pib"}, + {name: "invalid", unit: DataSizeUnit(-1), want: ""}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + assert.Equal(t, test.want, test.unit.String()) + }) + } +} + func TestDataSize_ConvertToUnit(t *testing.T) { tests := []struct { name string @@ -103,25 +131,25 @@ func TestDuration_String(t *testing.T) { d Duration want string }{ - "should successfully return valid seconds string": { + "should successfully return valid second string": { d: Duration{ Unit: Second, }, want: "s", }, - "should successfully return valid milliseconds string": { + "should successfully return valid millisecond string": { d: Duration{ Unit: Millisecond, }, want: "ms", }, - "should successfully return valid nanoseconds string": { + "should successfully return valid nanosecond string": { d: Duration{ Unit: Nanosecond, }, want: "ns", }, - "should successfully return valid microseconds string": { + "should successfully return valid microsecond string": { d: Duration{ Unit: Microsecond, }, @@ -163,7 +191,7 @@ func TestDuration_String(t *testing.T) { } func TestDuration_GoDuration(t *testing.T) { - t.Run("should successfully convert to GoDuration in seconds", func(t *testing.T) { + t.Run("should successfully convert to GoDuration in second", func(t *testing.T) { t.Parallel() d := Duration{ Value: 1, @@ -172,3 +200,86 @@ func TestDuration_GoDuration(t *testing.T) { assert.Equal(t, d.GoDuration(), 1*time.Second) }) } + +func TestDuration_UnmarshalBinary(t *testing.T) { + tests := map[string]struct { + input []byte + want DurationUnit + expectedErr error + }{ + "should successfully unmarshal binary from nanosecond": { + input: []byte("ns"), + want: Nanosecond, + }, + "should successfully unmarshal binary from microsecond": { + input: []byte("us"), + want: Microsecond, + }, + "should successfully unmarshal binary from millisecond": { + input: []byte("ms"), + want: Millisecond, + }, + "should successfully unmarshal binary from second": { + input: []byte("s"), + want: Second, + }, + "should successfully unmarshal binary from minute": { + input: []byte("min"), + want: Minute, + }, + "should successfully unmarshal binary from hour": { + input: []byte("h"), + want: Hour, + }, + "should successfully unmarshal binary from day": { + input: []byte("d"), + want: Day, + }, + "should fail unmarshal binary from unknown field": { + input: []byte("unknown"), + expectedErr: fmt.Errorf("unrecognized Duration unit: `unknown`"), + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + var unit DurationUnit + err := unit.UnmarshalBinary(tc.input) + + assert.Equal(t, tc.want, unit) + assert.Equal(t, tc.expectedErr, err) + }) + } +} + +func TestDataSizeUnit_UnmarshalBinary(t *testing.T) { + tests := []struct { + name string + input []byte + want DataSizeUnit + expectedErr error + }{ + {name: "byte", input: []byte("b"), want: Bytes}, + {name: "kilobyte", input: []byte("kb"), want: Kilobytes}, + {name: "kibibyte", input: []byte("kib"), want: Kibibytes}, + {name: "megabyte", input: []byte("mb"), want: Megabytes}, + {name: "mebibyte", input: []byte("mib"), want: Mebibytes}, + {name: "gigabyte", input: []byte("gb"), want: Gigabytes}, + {name: "gibibyte", input: []byte("gib"), want: Gibibytes}, + {name: "terabyte", input: []byte("tb"), want: Terabytes}, + {name: "tebibyte", input: []byte("tib"), want: Tebibytes}, + {name: "petabyte", input: []byte("pb"), want: Petabytes}, + {name: "pebibyte", input: []byte("pib"), want: Pebibytes}, + {name: "unknown", input: []byte("unknown"), expectedErr: fmt.Errorf("unrecognized DataSize unit: `unknown`")}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + var unit DataSizeUnit + err := unit.UnmarshalBinary(test.input) + + assert.Equal(t, test.want, unit) + assert.Equal(t, test.expectedErr, err) + }) + } +} From 2f71a986a655e2e5e2dfcfe3f63678c3550d335b Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 04:14:10 +0300 Subject: [PATCH 05/11] chore: implement error tests --- pkl/errors_test.go | 176 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 pkl/errors_test.go diff --git a/pkl/errors_test.go b/pkl/errors_test.go new file mode 100644 index 00000000..70d3a355 --- /dev/null +++ b/pkl/errors_test.go @@ -0,0 +1,176 @@ +//===----------------------------------------------------------------------===// +// Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. +// +// 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 +// +// https://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 pkl + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEvalError_Error(t *testing.T) { + tests := map[string]struct { + err EvalError + want string + }{ + "should return error output message": { + err: EvalError{ErrorOutput: "some eval error"}, + want: "some eval error", + }, + "should return empty string when error output is empty": { + err: EvalError{}, + want: "", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + assert.Equal(t, tc.want, tc.err.Error()) + }) + } +} + +func TestEvalError_Is(t *testing.T) { + tests := map[string]struct { + target error + want bool + }{ + "should return true when target is an EvalError": { + target: &EvalError{ErrorOutput: "some error"}, + want: true, + }, + "should return true when target is a different EvalError": { + target: &EvalError{ErrorOutput: "different error"}, + want: true, + }, + "should return false when target is nil": { + target: nil, + want: false, + }, + "should return false when target is a different error type": { + target: fmt.Errorf("some other error"), + want: false, + }, + "should return false when target is an InternalError": { + target: &InternalError{err: fmt.Errorf("internal")}, + want: false, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + evalErr := &EvalError{ErrorOutput: "eval error"} + assert.Equal(t, tc.want, evalErr.Is(tc.target)) + }) + } +} + +func TestInternalError_Error(t *testing.T) { + tests := map[string]struct { + err InternalError + want string + }{ + "should return formatted internal error message": { + err: InternalError{err: fmt.Errorf("something broke")}, + want: "an internal error occurred: something broke", + }, + "should return formatted message with nil inner error": { + err: InternalError{err: nil}, + want: "an internal error occurred: ", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + assert.Equal(t, tc.want, tc.err.Error()) + }) + } +} + +func TestInternalError_Is(t *testing.T) { + tests := map[string]struct { + target error + want bool + }{ + "should return true when target is an InternalError": { + target: &InternalError{err: fmt.Errorf("some error")}, + want: true, + }, + "should return true when target is a different InternalError": { + target: &InternalError{err: fmt.Errorf("different error")}, + want: true, + }, + "should return false when target is nil": { + target: nil, + want: false, + }, + "should return false when target is a different error type": { + target: fmt.Errorf("some other error"), + want: false, + }, + "should return false when target is an EvalError": { + target: &EvalError{ErrorOutput: "eval"}, + want: false, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + internalErr := &InternalError{err: fmt.Errorf("internal error")} + assert.Equal(t, tc.want, internalErr.Is(tc.target)) + }) + } +} + +func TestInternalError_Unwrap(t *testing.T) { + tests := map[string]struct { + inner error + want error + }{ + "should return the wrapped error": { + inner: fmt.Errorf("wrapped error"), + want: fmt.Errorf("wrapped error"), + }, + "should return nil when inner error is nil": { + inner: nil, + want: nil, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + internalErr := &InternalError{err: tc.inner} + assert.Equal(t, tc.want, internalErr.Unwrap()) + }) + } +} + +func TestEvalError_WorksWithErrorsIs(t *testing.T) { + t.Parallel() + evalErr := &EvalError{ErrorOutput: "eval error"} + wrappedErr := fmt.Errorf("wrapped: %w", evalErr) + assert.True(t, errors.Is(wrappedErr, &EvalError{})) +} + +func TestInternalError_WorksWithErrorsIs(t *testing.T) { + t.Parallel() + internalErr := &InternalError{err: fmt.Errorf("something broke")} + wrappedErr := fmt.Errorf("wrapped: %w", internalErr) + assert.True(t, errors.Is(wrappedErr, &InternalError{})) +} From 6aafc628ee8bbea51fe4a3abd40983b782b8fe82 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 04:22:32 +0300 Subject: [PATCH 06/11] chore: improve the functions declaration formatting in external reader --- pkl/external_reader.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkl/external_reader.go b/pkl/external_reader.go index 659bdcff..b19245da 100644 --- a/pkl/external_reader.go +++ b/pkl/external_reader.go @@ -53,21 +53,21 @@ type ExternalReaderClientOptions struct { } // WithExternalClientResourceReader adds an additional [ResourceReader] to the ExternalReaderClient. -var WithExternalClientResourceReader = func(reader ResourceReader) func(*ExternalReaderClientOptions) { +func WithExternalClientResourceReader(reader ResourceReader) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.ResourceReaders = append(options.ResourceReaders, reader) } } // WithExternalClientModuleReader adds an additional [ModuleReader] to the ExternalReaderClient. -var WithExternalClientModuleReader = func(reader ModuleReader) func(*ExternalReaderClientOptions) { +func WithExternalClientModuleReader(reader ModuleReader) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.ModuleReaders = append(options.ModuleReaders, reader) } } // WithExternalClientStreams sets the input and output interfaces the ExternalReaderClient will use to communicate with the Pkl evaluator. -var WithExternalClientStreams = func(requestReader io.Reader, responseWriter io.Writer) func(*ExternalReaderClientOptions) { +func WithExternalClientStreams(requestReader io.Reader, responseWriter io.Writer) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.RequestReader = requestReader options.ResponseWriter = responseWriter @@ -78,7 +78,7 @@ var WithExternalClientStreams = func(requestReader io.Reader, responseWriter io. // that associates the provided scheme with files from fs. // //goland:noinspection GoUnusedGlobalVariable -var WithExternalClientFs = func(fs fs.FS, scheme string) func(opts *ExternalReaderClientOptions) { +func WithExternalClientFs(fs fs.FS, scheme string) func(opts *ExternalReaderClientOptions) { return func(opts *ExternalReaderClientOptions) { reader := &fsReader{fs: fs, scheme: scheme} WithExternalClientModuleReader(&fsModuleReader{reader})(opts) From cae0762dbe949f5575457ffae5981b079ca2ad77 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 04:32:24 +0300 Subject: [PATCH 07/11] chore: implement unit tests for external_reader opts functions --- pkl/external_reader_test.go | 54 ++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/pkl/external_reader_test.go b/pkl/external_reader_test.go index f36c1a5e..37507120 100644 --- a/pkl/external_reader_test.go +++ b/pkl/external_reader_test.go @@ -1,5 +1,5 @@ //===----------------------------------------------------------------------===// -// Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. +// Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,10 +17,12 @@ package pkl import ( + "bytes" "context" "path/filepath" "runtime" "testing" + "testing/fstest" "github.com/apple/pkl-go/pkl/internal" "github.com/stretchr/testify/assert" @@ -38,6 +40,56 @@ fibErrB = test.catch(() -> read("fib:abc")) fibErrC = test.catch(() -> read("fib:-10")) ` +func TestWithExternalClientResourceReader(t *testing.T) { + t.Parallel() + reader := &fsResourceReader{&fsReader{fs: fstest.MapFS{}, scheme: "test"}} + opts := &ExternalReaderClientOptions{} + WithExternalClientResourceReader(reader)(opts) + + assert.NotNil(t, opts.ResourceReaders) + assert.Len(t, opts.ResourceReaders, 1) + assert.Equal(t, "test", opts.ResourceReaders[0].Scheme()) +} + +func TestWithExternalClientModuleReader(t *testing.T) { + t.Parallel() + reader := &fsModuleReader{&fsReader{fs: fstest.MapFS{}, scheme: "test"}} + opts := &ExternalReaderClientOptions{} + WithExternalClientModuleReader(reader)(opts) + + assert.NotNil(t, opts.ModuleReaders) + assert.Len(t, opts.ModuleReaders, 1) + assert.Equal(t, "test", opts.ModuleReaders[0].Scheme()) +} + +func TestWithExternalClientStreams(t *testing.T) { + t.Parallel() + requestReader := &bytes.Buffer{} + responseWriter := &bytes.Buffer{} + opts := &ExternalReaderClientOptions{} + WithExternalClientStreams(requestReader, responseWriter)(opts) + + assert.NotNil(t, opts.RequestReader) + assert.NotNil(t, opts.ResponseWriter) +} + +func TestWithExternalClientFs(t *testing.T) { + t.Parallel() + fsTest := fstest.MapFS{ + "hello.txt": &fstest.MapFile{Data: []byte("hello")}, + } + opts := &ExternalReaderClientOptions{} + WithExternalClientFs(fsTest, "fsTest")(opts) + + assert.NotNil(t, opts.ResourceReaders) + assert.Len(t, opts.ResourceReaders, 1) + assert.Equal(t, "fsTest", opts.ResourceReaders[0].Scheme()) + + assert.NotNil(t, opts.ModuleReaders) + assert.Len(t, opts.ModuleReaders, 1) + assert.Equal(t, "fsTest", opts.ModuleReaders[0].Scheme()) +} + func TestExternalReaderE2E(t *testing.T) { manager := NewEvaluatorManager() defer func() { _ = manager.Close() }() From a22dd594a4d5c243cd0f923df4687ee5bed73f7b Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Sun, 15 Mar 2026 04:36:12 +0300 Subject: [PATCH 08/11] chore: address copy right issue --- pkl/logger_test.go | 16 ++++++++++++++++ pkl/unmarshal_test.go | 2 +- pkl/values_test.go | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkl/logger_test.go b/pkl/logger_test.go index de493c87..64e1b656 100644 --- a/pkl/logger_test.go +++ b/pkl/logger_test.go @@ -1,3 +1,19 @@ +//===----------------------------------------------------------------------===// +// Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. +// +// 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 +// +// https://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 pkl import ( diff --git a/pkl/unmarshal_test.go b/pkl/unmarshal_test.go index f0295b41..15471565 100644 --- a/pkl/unmarshal_test.go +++ b/pkl/unmarshal_test.go @@ -1,5 +1,5 @@ //===----------------------------------------------------------------------===// -// Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. +// Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/pkl/values_test.go b/pkl/values_test.go index 089179cd..45020804 100644 --- a/pkl/values_test.go +++ b/pkl/values_test.go @@ -1,5 +1,5 @@ //===----------------------------------------------------------------------===// -// Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. +// Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 403f8cc2c5a2fbe4bc99ddd26c7e24c9e27935c3 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Tue, 17 Mar 2026 02:53:25 +0300 Subject: [PATCH 09/11] chore: format logger_test.go --- pkl/logger_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkl/logger_test.go b/pkl/logger_test.go index 64e1b656..53882c74 100644 --- a/pkl/logger_test.go +++ b/pkl/logger_test.go @@ -31,7 +31,6 @@ func (w writerMock) Write(_ []byte) (n int, err error) { } func TestLogger(t *testing.T) { - tests := map[string]struct { writerMock io.Writer msg string From 0fbd845dce05ea663248635c99bc51ae7afe61fc Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Tue, 17 Mar 2026 02:54:11 +0300 Subject: [PATCH 10/11] chore: revert external_reader --- pkl/external_reader.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkl/external_reader.go b/pkl/external_reader.go index b19245da..659bdcff 100644 --- a/pkl/external_reader.go +++ b/pkl/external_reader.go @@ -53,21 +53,21 @@ type ExternalReaderClientOptions struct { } // WithExternalClientResourceReader adds an additional [ResourceReader] to the ExternalReaderClient. -func WithExternalClientResourceReader(reader ResourceReader) func(*ExternalReaderClientOptions) { +var WithExternalClientResourceReader = func(reader ResourceReader) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.ResourceReaders = append(options.ResourceReaders, reader) } } // WithExternalClientModuleReader adds an additional [ModuleReader] to the ExternalReaderClient. -func WithExternalClientModuleReader(reader ModuleReader) func(*ExternalReaderClientOptions) { +var WithExternalClientModuleReader = func(reader ModuleReader) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.ModuleReaders = append(options.ModuleReaders, reader) } } // WithExternalClientStreams sets the input and output interfaces the ExternalReaderClient will use to communicate with the Pkl evaluator. -func WithExternalClientStreams(requestReader io.Reader, responseWriter io.Writer) func(*ExternalReaderClientOptions) { +var WithExternalClientStreams = func(requestReader io.Reader, responseWriter io.Writer) func(*ExternalReaderClientOptions) { return func(options *ExternalReaderClientOptions) { options.RequestReader = requestReader options.ResponseWriter = responseWriter @@ -78,7 +78,7 @@ func WithExternalClientStreams(requestReader io.Reader, responseWriter io.Writer // that associates the provided scheme with files from fs. // //goland:noinspection GoUnusedGlobalVariable -func WithExternalClientFs(fs fs.FS, scheme string) func(opts *ExternalReaderClientOptions) { +var WithExternalClientFs = func(fs fs.FS, scheme string) func(opts *ExternalReaderClientOptions) { return func(opts *ExternalReaderClientOptions) { reader := &fsReader{fs: fs, scheme: scheme} WithExternalClientModuleReader(&fsModuleReader{reader})(opts) From 95e479ee566b8423d56cb0506e4aec0da43a0ed2 Mon Sep 17 00:00:00 2001 From: NawafSwe Date: Tue, 17 Mar 2026 02:59:46 +0300 Subject: [PATCH 11/11] chore: revert logger and errors test and to be covered by e2e tests --- pkl/errors_test.go | 176 --------------------------------------------- pkl/logger_test.go | 54 -------------- 2 files changed, 230 deletions(-) delete mode 100644 pkl/errors_test.go delete mode 100644 pkl/logger_test.go diff --git a/pkl/errors_test.go b/pkl/errors_test.go deleted file mode 100644 index 70d3a355..00000000 --- a/pkl/errors_test.go +++ /dev/null @@ -1,176 +0,0 @@ -//===----------------------------------------------------------------------===// -// Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. -// -// 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 -// -// https://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 pkl - -import ( - "errors" - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestEvalError_Error(t *testing.T) { - tests := map[string]struct { - err EvalError - want string - }{ - "should return error output message": { - err: EvalError{ErrorOutput: "some eval error"}, - want: "some eval error", - }, - "should return empty string when error output is empty": { - err: EvalError{}, - want: "", - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - assert.Equal(t, tc.want, tc.err.Error()) - }) - } -} - -func TestEvalError_Is(t *testing.T) { - tests := map[string]struct { - target error - want bool - }{ - "should return true when target is an EvalError": { - target: &EvalError{ErrorOutput: "some error"}, - want: true, - }, - "should return true when target is a different EvalError": { - target: &EvalError{ErrorOutput: "different error"}, - want: true, - }, - "should return false when target is nil": { - target: nil, - want: false, - }, - "should return false when target is a different error type": { - target: fmt.Errorf("some other error"), - want: false, - }, - "should return false when target is an InternalError": { - target: &InternalError{err: fmt.Errorf("internal")}, - want: false, - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - evalErr := &EvalError{ErrorOutput: "eval error"} - assert.Equal(t, tc.want, evalErr.Is(tc.target)) - }) - } -} - -func TestInternalError_Error(t *testing.T) { - tests := map[string]struct { - err InternalError - want string - }{ - "should return formatted internal error message": { - err: InternalError{err: fmt.Errorf("something broke")}, - want: "an internal error occurred: something broke", - }, - "should return formatted message with nil inner error": { - err: InternalError{err: nil}, - want: "an internal error occurred: ", - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - assert.Equal(t, tc.want, tc.err.Error()) - }) - } -} - -func TestInternalError_Is(t *testing.T) { - tests := map[string]struct { - target error - want bool - }{ - "should return true when target is an InternalError": { - target: &InternalError{err: fmt.Errorf("some error")}, - want: true, - }, - "should return true when target is a different InternalError": { - target: &InternalError{err: fmt.Errorf("different error")}, - want: true, - }, - "should return false when target is nil": { - target: nil, - want: false, - }, - "should return false when target is a different error type": { - target: fmt.Errorf("some other error"), - want: false, - }, - "should return false when target is an EvalError": { - target: &EvalError{ErrorOutput: "eval"}, - want: false, - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - internalErr := &InternalError{err: fmt.Errorf("internal error")} - assert.Equal(t, tc.want, internalErr.Is(tc.target)) - }) - } -} - -func TestInternalError_Unwrap(t *testing.T) { - tests := map[string]struct { - inner error - want error - }{ - "should return the wrapped error": { - inner: fmt.Errorf("wrapped error"), - want: fmt.Errorf("wrapped error"), - }, - "should return nil when inner error is nil": { - inner: nil, - want: nil, - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - internalErr := &InternalError{err: tc.inner} - assert.Equal(t, tc.want, internalErr.Unwrap()) - }) - } -} - -func TestEvalError_WorksWithErrorsIs(t *testing.T) { - t.Parallel() - evalErr := &EvalError{ErrorOutput: "eval error"} - wrappedErr := fmt.Errorf("wrapped: %w", evalErr) - assert.True(t, errors.Is(wrappedErr, &EvalError{})) -} - -func TestInternalError_WorksWithErrorsIs(t *testing.T) { - t.Parallel() - internalErr := &InternalError{err: fmt.Errorf("something broke")} - wrappedErr := fmt.Errorf("wrapped: %w", internalErr) - assert.True(t, errors.Is(wrappedErr, &InternalError{})) -} diff --git a/pkl/logger_test.go b/pkl/logger_test.go deleted file mode 100644 index 53882c74..00000000 --- a/pkl/logger_test.go +++ /dev/null @@ -1,54 +0,0 @@ -//===----------------------------------------------------------------------===// -// Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. -// -// 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 -// -// https://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 pkl - -import ( - "io" - "testing" -) - -type writerMock struct { - err error - bytes int -} - -func (w writerMock) Write(_ []byte) (n int, err error) { - return w.bytes, w.err -} - -func TestLogger(t *testing.T) { - tests := map[string]struct { - writerMock io.Writer - msg string - frameURI string - }{ - "should successfully log a message as trace and warn": { - writerMock: writerMock{err: nil, bytes: 20}, - msg: "test message", - frameURI: "test", - }, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - t.Parallel() - lgr := NewLogger(tc.writerMock) - lgr.Trace(tc.msg, tc.frameURI) - lgr.Warn(tc.msg, tc.frameURI) - }) - } -}