Skip to content

Commit 3d53b56

Browse files
committed
Update
1 parent bc20880 commit 3d53b56

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

.github/scripts/split_spec.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#!/usr/bin/env bash
22
set -eou pipefail
33

4+
#########################################################
5+
# Run foas cli to split the openapi file
6+
# Variables:
7+
# target_env - target environment to split the openapi file
8+
#########################################################
9+
410
echo "Running FOAS CLI versions command"
511
foascli versions -s openapi-foas.json -o ./openapi/v2/versions.json --env "${target_env:?}" --stability-level stable --stability-level preview
612

713
echo "Running FOAS CLI split command with the following --env=${target_env:?} and -o=./openapi/v2/openapi.json"
814

9-
foascli split -s openapi-foas.json --env "${target_env:?}" -o ./openapi/v2/openapi.json
15+
foascli split -s openapi-foas.json --env "${target_env:?}" -o ./openapi/v2/openapi.json --format all
1016
mv -f "openapi-foas.json" "./openapi/v2.json"
11-
12-
foascli split -s openapi-foas.yaml --env "${target_env:?}" -o ./openapi/v2/openapi.yaml
1317
mv -f "openapi-foas.yaml" "./openapi/v2.yaml"
18+
1419
# Create folder if it does not exist
1520
mkdir -p ./openapi/v2/private
1621

tools/cli/internal/cli/split/split.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,20 @@ func (o *Opts) saveVersionedOas(oas *openapi3.T, version string) error {
9090
path = o.outputPath
9191
}
9292

93-
path = strings.Replace(path, "."+o.format, fmt.Sprintf("-%s.%s", version, o.format), 1)
93+
path = getVersionPath(path, version)
9494
return openapi.Save(path, oas, o.format, o.fs)
9595
}
9696

97+
// getVersionPath replaces file path from 'path/path.to.file/file.<json|yaml|any>'
98+
// to 'path/path.to.file/file-version.<json|yaml|any>'
99+
func getVersionPath(path, version string) string {
100+
extIndex := strings.LastIndex(path, ".")
101+
if extIndex == -1 {
102+
return fmt.Sprintf("%s-%s", path, version)
103+
}
104+
return fmt.Sprintf("%s-%s%s", path[:extIndex], version, path[extIndex:])
105+
}
106+
97107
func (o *Opts) PreRunE(_ []string) error {
98108
if o.basePath == "" {
99109
return fmt.Errorf("no OAS detected. Please, use the flag %s to include the base OAS", flag.Base)
@@ -128,7 +138,7 @@ func Builder() *cobra.Command {
128138
cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "-", usage.Spec)
129139
cmd.Flags().StringVar(&opts.env, flag.Environment, "", usage.Environment)
130140
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output)
131-
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.JSON, usage.Format)
141+
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.ALL, usage.Format)
132142
cmd.Flags().StringVar(&opts.gitSha, flag.GitSha, "", usage.GitSha)
133143

134144
_ = cmd.MarkFlagRequired(flag.Output)

tools/cli/test/e2e/cli/split_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,46 @@ func versionInFuture(t *testing.T, version string) bool {
112112
return v.Date().After(time.Now())
113113
}
114114

115+
func TestSplitVersionsFilteredOASes_All(t *testing.T) {
116+
cliPath := NewBin(t)
117+
env := "dev"
118+
folder := env
119+
base := getInputPath(t, "filtered", "json", folder)
120+
jsonOutputPath := getOutputFolder(t, folder) + "/filtered-dev-output.json"
121+
cmd := exec.Command(cliPath,
122+
"split",
123+
"-s",
124+
base,
125+
"-o",
126+
jsonOutputPath,
127+
"--env",
128+
"dev",
129+
"--format",
130+
"all",
131+
)
132+
133+
var o, e bytes.Buffer
134+
cmd.Stdout = &o
135+
cmd.Stderr = &e
136+
require.NoError(t, cmd.Run(), e.String())
137+
138+
versions := getVersions(t, cliPath, base, folder)
139+
for _, version := range versions {
140+
if slices.Contains(skipVersions, version) {
141+
continue
142+
}
143+
if env == "prod" && !versionInFuture(t, version) {
144+
continue
145+
}
146+
fmt.Printf("Validating version: %s\n", version)
147+
noExtensionOutputPath := strings.Replace(jsonOutputPath, ".json", "", 1)
148+
versionedOutputPath := noExtensionOutputPath + "-" + version + ".json"
149+
ValidateVersionedSpec(t, NewValidAtlasSpecPath(t, version, folder), versionedOutputPath)
150+
versionedOutputPath = noExtensionOutputPath + "-" + version + ".yaml"
151+
ValidateVersionedSpec(t, NewValidAtlasSpecPath(t, version, folder), versionedOutputPath)
152+
}
153+
}
154+
115155
func TestSplitVersionsForOASWithExternalReferences(t *testing.T) {
116156
folder := "dev"
117157
cliPath := NewBin(t)

0 commit comments

Comments
 (0)