Skip to content
Open
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
57 changes: 57 additions & 0 deletions clients/cli/spec/locales_download_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "spec_helper"

RSpec.describe "phrase locales download" do
let(:token) { "test-token-locales-download" }
let(:project_id) { "test-project-456" }
let(:locale_id) { "en-US" }

before do
mock_clear_requests!
end

# The API responds with 200 and an empty body when a --tags filter
# matches zero translations. This used to crash the generated
# "locales download" command with a nil pointer dereference, because
# it unconditionally called Close()/Name() on the (nil) result file.
it "does not crash when a tag filter matches no translations" do
mock_set!("GET", "/projects/#{project_id}/locales/#{locale_id}/download",
status: 200,
body: "",
headers: { "content-type" => "application/x-properties" }
)

r = run_cli(
"locales", "download",
"--id", locale_id,
"--project_id", project_id,
"--file_format", "properties",
"--tags", "nonexistent-tag",
"-t", token,
"--host", ENV.fetch("BASE_URL")
)

expect(r[:stderr]).not_to include("panic")
expect(r[:exit_code]).to eq(0)
expect(r[:stdout]).to eq("")
end

it "still prints the downloaded content for a normal response" do
mock_set!("GET", "/projects/#{project_id}/locales/#{locale_id}/download",
status: 200,
body: "hello=world",
headers: { "content-type" => "application/x-properties" }
)

r = run_cli(
"locales", "download",
"--id", locale_id,
"--project_id", project_id,
"--file_format", "properties",
"-t", token,
"--host", ENV.fetch("BASE_URL")
)

expect(r[:exit_code]).to eq(0)
expect(r[:stdout]).to include("hello=world")
end
end
10 changes: 6 additions & 4 deletions openapi-generator/templates/cli/api.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ func init{{{nickname}}}() {
HandleError(castedError)
}
} else if api_response.StatusCode >= 200 && api_response.StatusCode < 300 {
{{#if returnType}}{{#if (eq returnType "*os.File")}}content, _ := ioutil.ReadAll(data)
fmt.Printf("%s", string(content))
data.Close()
os.Remove(data.Name()){{else}}jsonBuf, jsonErr := json.MarshalIndent(data, "", " ")
{{#if returnType}}{{#if (eq returnType "*os.File")}}if data != nil {
content, _ := ioutil.ReadAll(data)
fmt.Printf("%s", string(content))
data.Close()
os.Remove(data.Name())
}{{else}}jsonBuf, jsonErr := json.MarshalIndent(data, "", " ")
if jsonErr != nil {
fmt.Printf("%v\n", data)
HandleError(err)
Expand Down
Loading