Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.64"
".": "0.1.0-alpha.65"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 20
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/stainless%2Fstainless-v0-6783cf45e0ea6644994eae08c41f755e29948bee313a6c2aaf5b710253eb4eaa.yml
openapi_spec_hash: b37f9ad1ca6bce774c6448b28d267bec
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/stainless%2Fstainless-v0-14899ed94f51eb27170c1618fcdb134c6df08db90996eeec2f620eb8d84c604a.yml
openapi_spec_hash: a4cf6948697a56d5b07ad48ef133093b
config_hash: f1b8a43873719fc8f2789008f3aa2260
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## 0.1.0-alpha.65 (2025-12-22)

Full Changelog: [v0.1.0-alpha.64...v0.1.0-alpha.65](https://github.com/stainless-api/stainless-api-cli/compare/v0.1.0-alpha.64...v0.1.0-alpha.65)

### Features

* added mock server tests ([f62abc2](https://github.com/stainless-api/stainless-api-cli/commit/f62abc2e06e2a0f85e57a22a956e3d9272fd65b2))


### Bug Fixes

* base64 encoding regression ([967d82c](https://github.com/stainless-api/stainless-api-cli/commit/967d82c469ec3f7aca4946c2f3bd4a32f848f7df))
* fix generated flag types and value wrapping ([b8293dc](https://github.com/stainless-api/stainless-api-cli/commit/b8293dcb107347ac57b692d540951b3c09350914))


### Chores

* **cli:** run pre-codegen tests on Windows ([7dd0ffb](https://github.com/stainless-api/stainless-api-cli/commit/7dd0ffba4c671678883eedb41b29dd1816b970da))
* **internal:** codegen related update ([c439ed6](https://github.com/stainless-api/stainless-api-cli/commit/c439ed63774cae542fa6eac8d01095a272061be9))
* **internal:** codegen related update ([f9e9d7d](https://github.com/stainless-api/stainless-api-cli/commit/f9e9d7dbcca54b2df0cde1c84e4bc65f525ef786))

## 0.1.0-alpha.64 (2025-12-17)

Full Changelog: [v0.1.0-alpha.63...v0.1.0-alpha.64](https://github.com/stainless-api/stainless-api-cli/compare/v0.1.0-alpha.63...v0.1.0-alpha.64)
Expand Down
3 changes: 3 additions & 0 deletions internal/binaryparam/binary_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestFileOrStdin(t *testing.T) {

stubStdin, err := os.Open(tempFile)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, stubStdin.Close()) })

readCloser, stdinInUse, err := FileOrStdin(stubStdin, "-")
require.NoError(t, err)
Expand All @@ -51,6 +52,7 @@ func TestFileOrStdin(t *testing.T) {

stubStdin, err := os.Open(tempFile)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, stubStdin.Close()) })

readCloser, stdinInUse, err := FileOrStdin(stubStdin, "/dev/fd/0")
require.NoError(t, err)
Expand All @@ -68,6 +70,7 @@ func TestFileOrStdin(t *testing.T) {

stubStdin, err := os.Open(tempFile)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, stubStdin.Close()) })

readCloser, stdinInUse, err := FileOrStdin(stubStdin, "/dev/stdin")
require.NoError(t, err)
Expand Down
96 changes: 96 additions & 0 deletions internal/mocktest/mocktest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package mocktest

import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var mockServerURL *url.URL

func init() {
mockServerURL, _ = url.Parse("http://localhost:4010")
if testURL := os.Getenv("TEST_API_BASE_URL"); testURL != "" {
if parsed, err := url.Parse(testURL); err == nil {
mockServerURL = parsed
}
}
}

// OnlyMockServerDialer only allows network connections to the mock server
type OnlyMockServerDialer struct{}

func (d *OnlyMockServerDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if address == mockServerURL.Host {
return (&net.Dialer{}).DialContext(ctx, network, address)
}

return nil, fmt.Errorf("BLOCKED: connection to %s not allowed (only allowed: %s)", address, mockServerURL.Host)
}

func blockNetworkExceptMockServer() (http.RoundTripper, http.RoundTripper) {
restricted := &http.Transport{
DialContext: (&OnlyMockServerDialer{}).DialContext,
}

origClient, origDefault := http.DefaultClient.Transport, http.DefaultTransport
http.DefaultClient.Transport, http.DefaultTransport = restricted, restricted
return origClient, origDefault
}

func restoreNetwork(origClient, origDefault http.RoundTripper) {
http.DefaultClient.Transport, http.DefaultTransport = origClient, origDefault
}

// TestRunMockTestWithFlags runs a test against a mock server with the provided
// CLI flags and ensures it succeeds
func TestRunMockTestWithFlags(t *testing.T, flags ...string) {
origClient, origDefault := blockNetworkExceptMockServer()
defer restoreNetwork(origClient, origDefault)

// Check if mock server is running
conn, err := net.DialTimeout("tcp", mockServerURL.Host, 2*time.Second)
if err != nil {
require.Fail(t, "Mock server is not running on "+mockServerURL.Host+". Please start the mock server before running tests.")
} else {
conn.Close()
}

// Get the path to the main command
_, filename, _, ok := runtime.Caller(0)
require.True(t, ok, "Could not get current file path")
dirPath := filepath.Dir(filename)
project := filepath.Join(dirPath, "..", "..", "cmd", "...")

args := []string{"run", project, "--base-url", mockServerURL.String()}
args = append(args, flags...)

t.Logf("Testing command: stl %s", strings.Join(args[4:], " "))

cmd := exec.Command("go", args...)
output, err := cmd.CombinedOutput()
if err != nil {
assert.Fail(t, "Test failed", "Error: %v\nOutput: %s", err, output)
}

t.Logf("Test passed successfully with output:\n%s\n", output)
}

func TestFile(t *testing.T, contents string) string {
tmpDir := t.TempDir()
filename := filepath.Join(tmpDir, "file.txt")
require.NoError(t, os.WriteFile(filename, []byte(contents), 0644))
return filename
}
3 changes: 2 additions & 1 deletion internal/requestflag/requestflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (d *DateValue) Parse(s string) error {
func (d *DateTimeValue) Parse(s string) error {
formats := []string{
time.RFC3339,
"2006-01-02T15:04:05Z07:00",
time.RFC3339Nano,
"2006-01-02T15:04:05",
"2006-01-02 15:04:05",
time.RFC1123,
Expand All @@ -584,6 +584,7 @@ func (d *DateTimeValue) Parse(s string) error {
func (t *TimeValue) Parse(s string) error {
formats := []string{
"15:04:05",
"15:04:05.999999999Z07:00",
"3:04:05PM",
"3:04 PM",
"15:04",
Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var buildsCreate = cli.Command{
Usage: "Optional commit message to use when creating a new commit.",
BodyPath: "commit_message",
},
&requestflag.Flag[any]{
&requestflag.Flag[map[string]string]{
Name: "target-commit-messages",
Usage: "Optional commit messages to use for each SDK when making a new commit.\nSDKs not represented in this object will fallback to the optional\n`commit_message` parameter, or will fallback further to the default\ncommit message.",
BodyPath: "target_commit_messages",
Expand Down Expand Up @@ -184,12 +184,12 @@ var buildsCompare = cli.Command{
Name: "compare",
Usage: "Create two builds whose outputs can be directly compared with each other.",
Flags: []cli.Flag{
&requestflag.Flag[any]{
&requestflag.Flag[map[string]any]{
Name: "base",
Usage: "Parameters for the base build",
BodyPath: "base",
},
&requestflag.Flag[any]{
&requestflag.Flag[map[string]any]{
Name: "head",
Usage: "Parameters for the head build",
BodyPath: "head",
Expand Down Expand Up @@ -246,16 +246,16 @@ func handleBuildsCreate(ctx context.Context, cmd *cli.Command) error {
if name, oas, err := convertFileFlag(cmd, "openapi-spec"); err != nil {
return err
} else if oas != nil {
modifyYAML(cmd, "revision", gjson.Escape("openapi"+path.Ext(name)), map[string][]byte{
"content": oas,
modifyYAML(cmd, "revision", gjson.Escape("openapi"+path.Ext(name)), map[string]string{
"content": string(oas),
})
}

if name, config, err := convertFileFlag(cmd, "stainless-config"); err != nil {
return err
} else if config != nil {
modifyYAML(cmd, "revision", gjson.Escape("stainless"+path.Ext(name)), map[string][]byte{
"content": config,
modifyYAML(cmd, "revision", gjson.Escape("stainless"+path.Ext(name)), map[string]string{
"content": string(config),
})
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/cmd/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package cmd

import (
"testing"

"github.com/stainless-api/stainless-api-cli/internal/mocktest"
)

func TestBuildsCreate(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds", "create",
"--project", "project",
"--revision", "string",
"--allow-empty",
"--branch", "branch",
"--commit-message", "commit_message",
"--target-commit-messages", "{cli: cli, csharp: csharp, go: go, java: java, kotlin: kotlin, node: node, openapi: openapi, php: php, python: python, ruby: ruby, terraform: terraform, typescript: typescript}",
"--target", "node",
)
}

func TestBuildsRetrieve(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds", "retrieve",
"--build-id", "buildId",
)
}

func TestBuildsList(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds", "list",
"--project", "project",
"--branch", "branch",
"--cursor", "cursor",
"--limit", "1",
"--revision", "string",
)
}

func TestBuildsCompare(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds", "compare",
"--base", "{branch: branch, revision: string, commit_message: commit_message}",
"--head", "{branch: branch, revision: string, commit_message: commit_message}",
"--project", "project",
"--target", "node",
)
}
22 changes: 22 additions & 0 deletions pkg/cmd/builddiagnostic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package cmd

import (
"testing"

"github.com/stainless-api/stainless-api-cli/internal/mocktest"
)

func TestBuildsDiagnosticsList(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds:diagnostics", "list",
"--build-id", "buildId",
"--cursor", "cursor",
"--limit", "1",
"--severity", "fatal",
"--targets", "targets",
)
}
21 changes: 21 additions & 0 deletions pkg/cmd/buildtargetoutput_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package cmd

import (
"testing"

"github.com/stainless-api/stainless-api-cli/internal/mocktest"
)

func TestBuildsTargetOutputsRetrieve(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"builds:target-outputs", "retrieve",
"--build-id", "build_id",
"--target", "node",
"--type", "source",
"--output", "url",
)
}
2 changes: 1 addition & 1 deletion pkg/cmd/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func getDefaultRequestOptions(cmd *cli.Command) []option.RequestOption {
case "staging":
opts = append(opts, option.WithEnvironmentStaging())
default:
log.Fatalf("Unknown environment: %s. Valid environments are: production, staging", environment)
log.Fatalf("Unknown environment: %s. Valid environments are %s", environment, "production, staging")
}
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/flagoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
EmptyBody BodyContentType = iota
MultipartFormEncoded
ApplicationJSON
ApplicationOctetStream
)

func flagOptions(
Expand Down Expand Up @@ -125,12 +126,23 @@ func flagOptions(
return nil, err
}
options = append(options, option.WithRequestBody(writer.FormDataContentType(), buf))

case ApplicationJSON:
bodyBytes, err := json.Marshal(bodyData)
if err != nil {
return nil, err
}
options = append(options, option.WithRequestBody("application/json", bodyBytes))

case ApplicationOctetStream:
if bodyBytes, ok := bodyData.([]byte); ok {
options = append(options, option.WithRequestBody("application/octet-stream", bodyBytes))
} else if bodyStr, ok := bodyData.(string); ok {
options = append(options, option.WithRequestBody("application/octet-stream", []byte(bodyStr)))
} else {
return nil, fmt.Errorf("Unsupported body for application/octet-stream: %v", bodyData)
}

default:
panic("Invalid body content type!")
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/cmd/org_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package cmd

import (
"testing"

"github.com/stainless-api/stainless-api-cli/internal/mocktest"
)

func TestOrgsRetrieve(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"orgs", "retrieve",
"--org", "org",
)
}

func TestOrgsList(t *testing.T) {
t.Skip("Prism tests are disabled")
mocktest.TestRunMockTestWithFlags(
t,
"orgs", "list",
)
}
Loading