package manifest_test import ( "strings" "testing" "git.vakhrushev.me/av/convy/internal/manifest" ) const projectSource = `# What this repository takes. source = "../conventions" # A component is a region where every chosen layer holds at once. [components.backend] dir = "backend/docs/conventions" lang = ["go"] topics = ["errors", "time"] [components.web] dir = "web/docs/conventions" topics = [ "client-logging", ] ` func TestAddToListKeepsTheShapeOfTheArray(t *testing.T) { out, err := manifest.AddToList([]byte(projectSource), "components.backend", "topics", "logging") if err != nil { t.Fatal(err) } body := string(out) if !strings.Contains(body, `topics = ["errors", "logging", "time"]`) { t.Errorf("a sorted one-line array did not stay sorted and on one line:\n%s", body) } if !strings.Contains(body, "# A component is a region where every chosen layer holds at once.") { t.Errorf("the comment did not survive the edit:\n%s", body) } out, err = manifest.AddToList(out, "components.web", "topics", "auth") if err != nil { t.Fatal(err) } want := "topics = [\n \"auth\",\n \"client-logging\",\n]" if !strings.Contains(string(out), want) { t.Errorf("an array written down a column did not stay a column:\n%s", out) } } func TestAddToListCreatesTheKeyItDoesNotFind(t *testing.T) { out, err := manifest.AddToList([]byte(projectSource), "components.web", "stack", "express") if err != nil { t.Fatal(err) } if !strings.Contains(string(out), `stack = ["express"]`) { t.Errorf("the missing key was not created:\n%s", out) } } func TestAddToListRefusesWhatIsThereAlready(t *testing.T) { _, err := manifest.AddToList([]byte(projectSource), "components.backend", "topics", "time") if err == nil || !strings.Contains(err.Error(), "already holds") { t.Errorf("a repeated subscription went through: %v", err) } if _, err := manifest.AddToList([]byte(projectSource), "components.mobile", "topics", "time"); err == nil { t.Errorf("a table that is not there took an entry") } } func TestRemoveFromList(t *testing.T) { out, err := manifest.RemoveFromList([]byte(projectSource), "components.backend", "topics", "errors") if err != nil { t.Fatal(err) } if !strings.Contains(string(out), `topics = ["time"]`) { t.Errorf("the value was not removed:\n%s", out) } if _, err := manifest.RemoveFromList([]byte(projectSource), "components.backend", "topics", "logging"); err == nil { t.Errorf("removing what is not there went through") } } func TestAddTableGoesLast(t *testing.T) { out, err := manifest.AddTable([]byte(projectSource), "components.mobile", [][2]string{{"dir", `"mobile/docs/conventions"`}, {"topics", "[]"}}) if err != nil { t.Fatal(err) } body := string(out) if !strings.Contains(body, "[components.mobile]\ndir = \"mobile/docs/conventions\"\ntopics = []") { t.Errorf("the table was not written:\n%s", body) } if strings.Index(body, "[components.mobile]") < strings.Index(body, "[components.web]") { t.Errorf("the new table did not go last:\n%s", body) } if _, err := manifest.AddTable(out, "components.mobile", nil); err == nil { t.Errorf("a second table of the same name went through") } }