Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab6c90d

Browse files
committedNov 9, 2022
Migrate TestCoreSearchSortedResults from test_core.py to core_test.go
1 parent f7618d6 commit ab6c90d

File tree

2 files changed

+73
-44
lines changed

2 files changed

+73
-44
lines changed
 

‎internal/integrationtest/core/core_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core_test
1818
import (
1919
"fmt"
2020
"runtime"
21+
"sort"
2122
"strconv"
2223
"strings"
2324
"testing"
@@ -582,3 +583,75 @@ func TestCoreListWithInstalledJson(t *testing.T) {
582583
}
583584
]`)
584585
}
586+
587+
func TestCoreSearchSortedResults(t *testing.T) {
588+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
589+
defer env.CleanUp()
590+
591+
// Set up the server to serve our custom index file
592+
testIndex := paths.New("..", "testdata", "test_index.json")
593+
url := env.HTTPServeFile(8000, testIndex)
594+
595+
// update custom index
596+
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
597+
require.NoError(t, err)
598+
599+
// This is done only to avoid index update output when calling core search
600+
// since it automatically updates them if they're outdated and it makes it
601+
// harder to parse the list of cores
602+
_, _, err = cli.Run("core", "search")
603+
require.NoError(t, err)
604+
605+
// list all with additional url specified
606+
stdout, _, err := cli.Run("core", "search", "--additional-urls="+url.String())
607+
require.NoError(t, err)
608+
609+
out := strings.Split(strings.TrimSpace(string(stdout)), "\n")
610+
var lines, deprecated, notDeprecated [][]string
611+
for i, v := range out {
612+
if i > 0 {
613+
v = strings.Join(strings.Fields(v), " ")
614+
lines = append(lines, strings.SplitN(v, " ", 3))
615+
}
616+
}
617+
for _, v := range lines {
618+
if strings.HasPrefix(v[2], "[DEPRECATED]") {
619+
deprecated = append(deprecated, v)
620+
} else {
621+
notDeprecated = append(notDeprecated, v)
622+
}
623+
}
624+
625+
// verify that results are already sorted correctly
626+
require.True(t, sort.SliceIsSorted(deprecated, func(i, j int) bool {
627+
return strings.ToLower(deprecated[i][2]) < strings.ToLower(deprecated[j][2])
628+
}))
629+
require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool {
630+
return strings.ToLower(notDeprecated[i][2]) < strings.ToLower(notDeprecated[j][2])
631+
}))
632+
633+
// verify that deprecated platforms are the last ones
634+
require.Equal(t, lines, append(notDeprecated, deprecated...))
635+
636+
// test same behaviour with json output
637+
stdout, _, err = cli.Run("core", "search", "--additional-urls="+url.String(), "--format=json")
638+
require.NoError(t, err)
639+
640+
// verify that results are already sorted correctly
641+
sortedDeprecated := requirejson.Parse(t, stdout).Query(
642+
"[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort").String()
643+
notSortedDeprecated := requirejson.Parse(t, stdout).Query(
644+
"[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]").String()
645+
require.Equal(t, sortedDeprecated, notSortedDeprecated)
646+
647+
sortedNotDeprecated := requirejson.Parse(t, stdout).Query(
648+
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String()
649+
notSortedNotDeprecated := requirejson.Parse(t, stdout).Query(
650+
"[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String()
651+
require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated)
652+
653+
// verify that deprecated platforms are the last ones
654+
platform := requirejson.Parse(t, stdout).Query(
655+
"[.[] | .name |=ascii_downcase | .name]").String()
656+
require.True(t, strings.HasSuffix(platform, strings.TrimLeft(notSortedDeprecated, "[")))
657+
}

‎test/test_core.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -93,50 +93,6 @@ def test_core_search_update_index_delay(run_command, data_dir):
9393
assert "Downloading index" not in res.stdout
9494

9595

96-
def test_core_search_sorted_results(run_command, httpserver):
97-
# Set up the server to serve our custom index file
98-
test_index = Path(__file__).parent / "testdata" / "test_index.json"
99-
httpserver.expect_request("/test_index.json").respond_with_data(test_index.read_text())
100-
101-
# update custom index
102-
url = httpserver.url_for("/test_index.json")
103-
assert run_command(["core", "update-index", f"--additional-urls={url}"])
104-
105-
# This is done only to avoid index update output when calling core search
106-
# since it automatically updates them if they're outdated and it makes it
107-
# harder to parse the list of cores
108-
assert run_command(["core", "search"])
109-
110-
# list all with additional url specified
111-
result = run_command(["core", "search", f"--additional-urls={url}"])
112-
assert result.ok
113-
114-
lines = [l.strip().split(maxsplit=2) for l in result.stdout.strip().splitlines()][1:]
115-
not_deprecated = [l for l in lines if not l[2].startswith("[DEPRECATED]")]
116-
deprecated = [l for l in lines if l[2].startswith("[DEPRECATED]")]
117-
118-
# verify that results are already sorted correctly
119-
assert not_deprecated == sorted(not_deprecated, key=lambda tokens: tokens[2].lower())
120-
assert deprecated == sorted(deprecated, key=lambda tokens: tokens[2].lower())
121-
122-
# verify that deprecated platforms are the last ones
123-
assert lines == not_deprecated + deprecated
124-
125-
# test same behaviour with json output
126-
result = run_command(["core", "search", f"--additional-urls={url}", "--format=json"])
127-
assert result.ok
128-
129-
platforms = json.loads(result.stdout)
130-
not_deprecated = [p for p in platforms if not p.get("deprecated")]
131-
deprecated = [p for p in platforms if p.get("deprecated")]
132-
133-
# verify that results are already sorted correctly
134-
assert not_deprecated == sorted(not_deprecated, key=lambda keys: keys["name"].lower())
135-
assert deprecated == sorted(deprecated, key=lambda keys: keys["name"].lower())
136-
# verify that deprecated platforms are the last ones
137-
assert platforms == not_deprecated + deprecated
138-
139-
14096
def test_core_list_sorted_results(run_command, httpserver):
14197
# Set up the server to serve our custom index file
14298
test_index = Path(__file__).parent / "testdata" / "test_index.json"

0 commit comments

Comments
 (0)
Please sign in to comment.