Skip to content

Commit 0a4d3da

Browse files
Migrate TestCoreListSortedResults from test_core.py to core_test.go
1 parent 1f5384e commit 0a4d3da

File tree

2 files changed

+73
-46
lines changed

2 files changed

+73
-46
lines changed

internal/integrationtest/core/core_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,76 @@ func TestCoreSearchSortedResults(t *testing.T) {
640640
"[.[] | .name |=ascii_downcase | .name]").String()
641641
require.True(t, strings.HasSuffix(platform, strings.TrimLeft(notSortedDeprecated, "[")))
642642
}
643+
644+
func TestCoreListSortedResults(t *testing.T) {
645+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
646+
defer env.CleanUp()
647+
648+
// Set up the server to serve our custom index file
649+
testIndex := paths.New("..", "testdata", "test_index.json")
650+
url := env.HTTPServeFile(8000, testIndex)
651+
652+
// update custom index
653+
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
654+
require.NoError(t, err)
655+
656+
// install some core for testing
657+
_, _, err = cli.Run("core", "install", "test:x86", "Retrokits-RK002:arm", "Package:x86", "--additional-urls="+url.String())
658+
require.NoError(t, err)
659+
660+
// list all with additional url specified
661+
stdout, _, err := cli.Run("core", "list", "--additional-urls="+url.String())
662+
require.NoError(t, err)
663+
664+
out := strings.Split(strings.TrimSpace(string(stdout)), "\n")
665+
var lines, deprecated, notDeprecated [][]string
666+
for i, v := range out {
667+
if i > 0 {
668+
v = strings.Join(strings.Fields(v), " ")
669+
lines = append(lines, strings.SplitN(v, " ", 4))
670+
}
671+
}
672+
require.Len(t, lines, 3)
673+
for _, v := range lines {
674+
if strings.HasPrefix(v[3], "[DEPRECATED]") {
675+
deprecated = append(deprecated, v)
676+
} else {
677+
notDeprecated = append(notDeprecated, v)
678+
}
679+
}
680+
681+
// verify that results are already sorted correctly
682+
require.True(t, sort.SliceIsSorted(deprecated, func(i, j int) bool {
683+
return strings.ToLower(deprecated[i][3]) < strings.ToLower(deprecated[j][3])
684+
}))
685+
require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool {
686+
return strings.ToLower(notDeprecated[i][3]) < strings.ToLower(notDeprecated[j][3])
687+
}))
688+
689+
// verify that deprecated platforms are the last ones
690+
require.Equal(t, lines, append(notDeprecated, deprecated...))
691+
692+
// test same behaviour with json output
693+
stdout, _, err = cli.Run("core", "list", "--additional-urls="+url.String(), "--format=json")
694+
require.NoError(t, err)
695+
requirejson.Len(t, stdout, 3)
696+
697+
// verify that results are already sorted correctly
698+
sortedDeprecated := requirejson.Parse(t, stdout).Query(
699+
"[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort").String()
700+
notSortedDeprecated := requirejson.Parse(t, stdout).Query(
701+
"[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]").String()
702+
require.Equal(t, sortedDeprecated, notSortedDeprecated)
703+
704+
sortedNotDeprecated := requirejson.Parse(t, stdout).Query(
705+
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String()
706+
notSortedNotDeprecated := requirejson.Parse(t, stdout).Query(
707+
"[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String()
708+
require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated)
709+
710+
// verify that deprecated platforms are the last ones
711+
platform := requirejson.Parse(t, stdout).Query(
712+
"[.[] | .name |=ascii_downcase | .name]").String()
713+
require.True(t, strings.HasSuffix(platform, strings.TrimLeft(notSortedDeprecated, "[")))
714+
715+
}

test/test_core.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,52 +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_list_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-
# install some core for testing
106-
assert run_command(
107-
["core", "install", "test:x86", "Retrokits-RK002:arm", "Package:x86", f"--additional-urls={url}"]
108-
)
109-
110-
# list all with additional url specified
111-
result = run_command(["core", "list", f"--additional-urls={url}"])
112-
assert result.ok
113-
114-
lines = [l.strip().split(maxsplit=3) for l in result.stdout.strip().splitlines()][1:]
115-
assert len(lines) == 3
116-
not_deprecated = [l for l in lines if not l[3].startswith("[DEPRECATED]")]
117-
deprecated = [l for l in lines if l[3].startswith("[DEPRECATED]")]
118-
119-
# verify that results are already sorted correctly
120-
assert not_deprecated == sorted(not_deprecated, key=lambda tokens: tokens[3].lower())
121-
assert deprecated == sorted(deprecated, key=lambda tokens: tokens[3].lower())
122-
123-
# verify that deprecated platforms are the last ones
124-
assert lines == not_deprecated + deprecated
125-
126-
# test same behaviour with json output
127-
result = run_command(["core", "list", f"--additional-urls={url}", "--format=json"])
128-
assert result.ok
129-
130-
platforms = json.loads(result.stdout)
131-
assert len(platforms) == 3
132-
not_deprecated = [p for p in platforms if not p.get("deprecated")]
133-
deprecated = [p for p in platforms if p.get("deprecated")]
134-
135-
# verify that results are already sorted correctly
136-
assert not_deprecated == sorted(not_deprecated, key=lambda keys: keys["name"].lower())
137-
assert deprecated == sorted(deprecated, key=lambda keys: keys["name"].lower())
138-
# verify that deprecated platforms are the last ones
139-
assert platforms == not_deprecated + deprecated
140-
141-
14296
def test_core_list_deprecated_platform_with_installed_json(run_command, httpserver, data_dir):
14397
# Set up the server to serve our custom index file
14498
test_index = Path(__file__).parent / "testdata" / "test_index.json"

0 commit comments

Comments
 (0)