Skip to content

Add types modification capability to modify command #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2021
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
1 change: 1 addition & 0 deletions internal/cli/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Modify the registration data of library name LIBRARY_NAME according to the FLAGs

func init() {
modifyCmd.Flags().String("repo-url", "", "New library repository URL")
modifyCmd.Flags().String("types", "", "New types list for the library's releases (comma separated)")

rootCmd.AddCommand(modifyCmd)
}
58 changes: 58 additions & 0 deletions internal/command/modify/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package modify
import (
"fmt"
"os"
"strings"

"github.com/arduino/go-paths-helper"
"github.com/arduino/libraries-repository-engine/internal/backup"
Expand Down Expand Up @@ -121,6 +122,10 @@ func modifications(flags *pflag.FlagSet) (bool, error) {
if err != nil {
return false, err
}
newTypes, err := flags.GetString("types")
if err != nil {
return false, err
}

if newRepositoryURL != "" {
if err := modifyRepositoryURL(newRepositoryURL); err != nil {
Expand All @@ -130,6 +135,14 @@ func modifications(flags *pflag.FlagSet) (bool, error) {
didModify = true
}

if newTypes != "" {
if err := modifyTypes(newTypes); err != nil {
return false, err
}

didModify = true
}

if !didModify {
return false, fmt.Errorf("No modification flags provided so nothing happened. See 'libraries-repository-engine modify --help'")
}
Expand Down Expand Up @@ -215,3 +228,48 @@ func modifyRepositoryURL(newRepositoryURL string) error {

return nil
}

func modifyTypes(rawTypes string) error {
newTypes := strings.Split(rawTypes, ",")
for i := range newTypes {
newTypes[i] = strings.TrimSpace(newTypes[i])
}

sameTypes := func(oldTypes []string) bool {
if len(oldTypes) != len(newTypes) {
return false
}

for _, oldType := range oldTypes {
found := false
for _, newType := range newTypes {
if oldType == newType {
found = true
break
}
}
if !found {
return false
}
}

return true
}

typesChanged := false

for _, releaseData := range releasesData {
if !typesChanged {
// Compare old and new types for this release
typesChanged = !sameTypes(releaseData.Types)
}

releaseData.Types = newTypes
}

if !typesChanged {
return fmt.Errorf("Library %s already has types %s", libraryName, rawTypes)
}

return nil
}
62 changes: 62 additions & 0 deletions test/test_modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,65 @@ def get_release_archive_url(name, version):
)
assert canary_release_archive_path.exists()
assert get_release_archive_url(name=canary_name, version=canary_release) == canary_release_archive_url


def test_types(configuration, run_command):
"""Test the `--types` modification flag in action."""
name = "SpacebrewYun"
raw_old_types = "Arduino"
raw_new_types = "Arduino, Retired"
canary_name = "Arduino Uno WiFi Dev Ed Library"
raw_canary_types = "Partner"
# Run the sync command to generate test data
engine_command = [
"sync",
"--config-file",
configuration.path,
test_data_path.joinpath("test_modify", "test_types", "repos.txt"),
]
result = run_command(cmd=engine_command)
assert result.ok
assert pathlib.Path(configuration.data["LibrariesDB"]).exists()

def assert_types(name, raw_types):
with pathlib.Path(configuration.data["LibrariesDB"]).open(mode="r", encoding="utf-8") as library_db_file:
library_db = json.load(fp=library_db_file)
for release in library_db["Releases"]:
if release["LibraryName"] == name and release["Types"] != [
raw_type.strip() for raw_type in raw_types.split(sep=",")
]:
return False
return True

# Verify the pre-command DB is as expected
assert assert_types(name=name, raw_types=raw_old_types)
assert assert_types(name=canary_name, raw_types=raw_canary_types)

# Run the modification command with existing types
engine_command = [
"modify",
"--config-file",
configuration.path,
"--types",
raw_old_types,
name,
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"{name} already has types {raw_old_types}" in result.stderr

# Run the modification command with existing types
engine_command = [
"modify",
"--config-file",
configuration.path,
"--types",
raw_new_types,
name,
]
result = run_command(cmd=engine_command)
assert result.ok

# Verify the effect of the command was as expected
assert assert_types(name=name, raw_types=raw_new_types)
assert assert_types(name=canary_name, raw_types=raw_canary_types)
2 changes: 2 additions & 0 deletions test/testdata/test_modify/test_types/repos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/arduino-libraries/SpacebrewYun.git|Arduino|SpacebrewYun
https://github.com/arduino-libraries/UnoWiFi-Developer-Edition-Lib.git|Partner|Arduino Uno WiFi Dev Ed Library