Skip to content

Commit 1160ca4

Browse files
committed
Validation moved to languagecontainer
1 parent d7ce698 commit 1160ca4

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

internal/librariangen/generate/generator.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var (
4141
// Generate is the main entrypoint for the `generate` command. It orchestrates
4242
// the entire generation process.
4343
func Generate(ctx context.Context, cfg *generate.Config) error {
44-
if err := cfg.Context.Validate(); err != nil {
45-
return fmt.Errorf("librariangen: invalid configuration: %w", err)
46-
}
4744
slog.Debug("librariangen: generate command started")
4845
defer cleanupIntermediateFiles(cfg.Context.OutputDir)
4946

internal/librariangen/languagecontainer/generate/generate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package generate
1717

1818
import (
1919
"errors"
20+
"fmt"
2021
"log/slog"
2122
"path/filepath"
2223

@@ -68,6 +69,9 @@ type Config struct {
6869
// NewConfig creates a new Config, parsing the generate-request.json file
6970
// from the LibrarianDir in the given Context.
7071
func NewConfig(ctx *Context) (*Config, error) {
72+
if err := ctx.Validate(); err != nil {
73+
return nil, fmt.Errorf("invalid context: %w", err)
74+
}
7175
reqPath := filepath.Join(ctx.LibrarianDir, "generate-request.json")
7276
slog.Debug("languagecontainer: reading generate request", "path", reqPath)
7377

internal/librariangen/languagecontainer/generate/generate_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func TestNewConfig(t *testing.T) {
2828
want := &Config{
2929
Context: &Context{
3030
LibrarianDir: librarianDir,
31+
InputDir: "in",
32+
OutputDir: "out",
33+
SourceDir: "source",
3134
},
3235
Request: &message.Library{
3336
ID: "chronicle",
@@ -55,6 +58,9 @@ func TestNewConfig(t *testing.T) {
5558

5659
ctx := &Context{
5760
LibrarianDir: librarianDir,
61+
InputDir: "in",
62+
OutputDir: "out",
63+
SourceDir: "source",
5864
}
5965
got, err := NewConfig(ctx)
6066
if err != nil {
@@ -65,3 +71,43 @@ func TestNewConfig(t *testing.T) {
6571
t.Errorf("NewConfig() mismatch (-want +got):\n%s", diff)
6672
}
6773
}
74+
75+
func TestNewConfig_validate(t *testing.T) {
76+
tests := []struct {
77+
name string
78+
context *Context
79+
}{
80+
{
81+
name: "empty librarian dir",
82+
context: &Context{},
83+
},
84+
{
85+
name: "empty input dir",
86+
context: &Context{
87+
LibrarianDir: "librarian",
88+
},
89+
},
90+
{
91+
name: "empty output dir",
92+
context: &Context{
93+
LibrarianDir: "librarian",
94+
InputDir: "in",
95+
},
96+
},
97+
{
98+
name: "empty source dir",
99+
context: &Context{
100+
LibrarianDir: "librarian",
101+
InputDir: "in",
102+
OutputDir: "out",
103+
},
104+
},
105+
}
106+
for _, tt := range tests {
107+
t.Run(tt.name, func(t *testing.T) {
108+
if _, err := NewConfig(tt.context); err == nil {
109+
t.Error("NewConfig() error = nil, want not nil")
110+
}
111+
})
112+
}
113+
}

internal/librariangen/languagecontainer/languagecontainer_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
5-
// You may not use this file except in compliance with the License.
65
// You may obtain a copy of the License at
76
//
87
// http://www.apache.org/licenses/LICENSE-2.0

internal/librariangen/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ func runCLI(args []string) int {
5252
Generate: generate.Generate,
5353
ReleaseInit: release.Init,
5454
}
55-
if exitCode := languagecontainer.Run(args[1:], &container); exitCode != 0 {
56-
slog.Error("command failed", "command", args[1])
57-
return 1
58-
}
59-
60-
slog.Info("librariangen: finished successfully")
61-
return 0
55+
return languagecontainer.Run(args, &container)
6256
}
6357

6458
func parseLogLevel(logLevelEnv string) slog.Level {

0 commit comments

Comments
 (0)