Skip to content

Commit

Permalink
Merge pull request #197 from transifex/TX-14394_add_remote_skip_native
Browse files Browse the repository at this point in the history
Add Remote sources should skip  Native Projects
  • Loading branch information
foteinigk authored Jul 26, 2023
2 parents 05c3163 + f755451 commit 5acb442
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
6 changes: 2 additions & 4 deletions internal/txlib/add_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ func AddRemoteCommand(
}
i18nFormat, exists := i18nFormats[i18nFormatRelationship.DataSingular.Id]
if !exists {
return fmt.Errorf(
"could not find file Format: %s",
resource.Relationships["i18n_format"].DataSingular.Id,
)
fmt.Printf("Resource %s skipped: Invalid file Format %s\n", resource.Id, i18nFormatRelationship.DataSingular.Id)
continue
}

// Construct file-filter
Expand Down
78 changes: 78 additions & 0 deletions internal/txlib/add_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package txlib

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"reflect"
"strings"
"testing"

"github.com/transifex/cli/internal/txlib/config"
Expand Down Expand Up @@ -82,3 +84,79 @@ func TestAddRemote(t *testing.T) {
t.Errorf("Got request '%+v', expected '%+v'", actual, expected)
}
}

func TestAddRemoteInvalidFileFormat(t *testing.T) {
curDir, _ := os.Getwd()
tempDir, _ := os.MkdirTemp("", "")
defer os.RemoveAll(tempDir)
_ = os.Chdir(tempDir)
defer os.Chdir(curDir)

resourcesUrl := fmt.Sprintf(
"/resources?%s=%s",
url.QueryEscape("filter[project]"),
url.QueryEscape(projectId),
)
i18nFormatsUrl := fmt.Sprintf(
"/i18n_formats?%s=%s",
url.QueryEscape("filter[organization]"),
url.QueryEscape("o:orgslug"),
)

// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

mockData := jsonapi.MockData{
projectUrl: getProjectEndpoint(),
resourcesUrl: jsonapi.GetMockTextResponse(
`{"data": [{
"type": "resources",
"id": "o:orgslug:p:projslug:r:resslug",
"attributes": {"slug": "resslug"},
"relationships": {
"i18n_format": {"data": {"type": "i18n_formats", "id": "FILELESS"}}
}
}]}`,
),
i18nFormatsUrl: jsonapi.GetMockTextResponse(
`{"data": [{
"type": "i18n_formats",
"id": "PO",
"attributes": {"file_extensions": [".po"]}
}]}`,
),
}

api := jsonapi.GetTestConnection(mockData)
cfg := &config.Config{Local: &config.LocalConfig{}}

err := AddRemoteCommand(
cfg,
&api,
"https://app.transifex.com/orgslug/projslug/whatever/whatever/",
// Lets make the file filter a bit weird
"locale/<project_slug><project_slug>.<resource_slug>/<lang>.<ext>",
50,
)
if err != nil {
t.Errorf("%s", err)
}

// Restore stdout
w.Close()
os.Stdout = oldStdout
out, _ := ioutil.ReadAll(r)
r.Close()

testSimpleGet(t, mockData, projectUrl)
testSimpleGet(t, mockData, resourcesUrl)
testSimpleGet(t, mockData, i18nFormatsUrl)

// Check if the expected error message was printed
expectedErrorMessage := "Resource o:orgslug:p:projslug:r:resslug skipped: Invalid file Format FILELESS"
if !strings.Contains(string(out), expectedErrorMessage) {
t.Errorf("Expected error message '%s' not found in printed output", expectedErrorMessage)
}
}

0 comments on commit 5acb442

Please sign in to comment.