Skip to content

Commit

Permalink
fix(lang): Fix vscode plugin parse logic (#123)
Browse files Browse the repository at this point in the history
* fix(lang): Fix vscode plugin parse logic

Signed-off-by: Ce Gao <[email protected]>

* fix: Add more test cases

Signed-off-by: Ce Gao <[email protected]>
  • Loading branch information
gaocegege authored May 10, 2022
1 parent 22f8fb9 commit d6aec6f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/builder/builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
. "github.com/onsi/gomega"
)

func TestOci(t *testing.T) {
func TestBuilder(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Builder Suite")
}
File renamed without changes.
27 changes: 27 additions & 0 deletions pkg/editor/vscode/vscode_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 The MIDI Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package vscode

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestVSCode(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "VS Code Suite")
}
81 changes: 81 additions & 0 deletions pkg/editor/vscode/vscode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 The MIDI Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vscode

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Visual Studio Code", func() {
Describe("Plugin", func() {
It("should be able to parse", func() {
tcs := []struct {
name string
expectedExtension string
expectedVersion string
expectedPublisher string
expectedErr bool
}{
{
name: "ms-python.python-2021.12.1559732655",
expectedPublisher: "ms-python",
expectedExtension: "python",
expectedVersion: "2021.12.1559732655",
expectedErr: false,
},
{
name: "ms-vscode.cpptools-1.7.1",
expectedPublisher: "ms-vscode",
expectedExtension: "cpptools",
expectedVersion: "1.7.1",
expectedErr: false,
},
{
name: "github.copilot-1.12.5517",
expectedPublisher: "github",
expectedExtension: "copilot",
expectedVersion: "1.12.5517",
expectedErr: false,
},
{
name: "dbaeumer.vscode-eslint-1.1.1",
expectedPublisher: "dbaeumer",
expectedExtension: "vscode-eslint",
expectedVersion: "1.1.1",
expectedErr: false,
},
{
name: "test",
expectedErr: true,
},
{
name: "test.test",
expectedErr: true,
},
}
for _, tc := range tcs {
p, err := ParsePlugin(tc.name)
if tc.expectedErr {
Expect(err).To(HaveOccurred())
} else {
Expect(err).ToNot(HaveOccurred())
Expect(p.Publisher).To(Equal(tc.expectedPublisher))
Expect(p.Extension).To(Equal(tc.expectedExtension))
Expect(p.Version).To(Equal(tc.expectedVersion))
}
}
})
})
})
15 changes: 12 additions & 3 deletions pkg/vscode/vsocde.go → pkg/editor/vscode/vsocde.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ func (c generalClient) DownloadOrCache(p Plugin) error {
return nil
}

func ParsePlugin(p string) (Plugin, error) {
func ParsePlugin(p string) (*Plugin, error) {
indexPublisher := strings.Index(p, ".")
if indexPublisher == -1 {
return nil, errors.New("invalid publisher")
}
publisher := p[:indexPublisher]
indexExtension := strings.Index(p[indexPublisher:], "-") + indexPublisher

indexExtension := strings.LastIndex(p[indexPublisher:], "-")
if indexExtension == -1 {
return nil, errors.New("invalid extension")
}

indexExtension = indexPublisher + indexExtension
extension := p[indexPublisher+1 : indexExtension]
version := p[indexExtension+1:]
logrus.WithFields(logrus.Fields{
"publisher": publisher,
"extension": extension,
"version": version,
}).Debug("vscode plugin is parsed")
return Plugin{
return &Plugin{
Publisher: publisher,
Extension: extension,
Version: version,
Expand Down
3 changes: 2 additions & 1 deletion pkg/lang/ir/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"github.com/moby/buildkit/client/llb"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/tensorchord/MIDI/pkg/editor/vscode"
"github.com/tensorchord/MIDI/pkg/flag"
"github.com/tensorchord/MIDI/pkg/shell"
"github.com/tensorchord/MIDI/pkg/vscode"
)

func NewGraph() *Graph {
Expand Down
4 changes: 2 additions & 2 deletions pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ir
import (
"errors"

"github.com/tensorchord/MIDI/pkg/vscode"
"github.com/tensorchord/MIDI/pkg/editor/vscode"
)

func Base(os, language string) {
Expand All @@ -30,7 +30,7 @@ func VSCodePlugins(plugins []string) error {
if err != nil {
return err
}
DefaultGraph.VSCodePlugins = append(DefaultGraph.VSCodePlugins, plugin)
DefaultGraph.VSCodePlugins = append(DefaultGraph.VSCodePlugins, *plugin)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package ir

import (
"github.com/tensorchord/MIDI/pkg/vscode"
"github.com/tensorchord/MIDI/pkg/editor/vscode"
)

// A Graph contains the state,
Expand Down

0 comments on commit d6aec6f

Please sign in to comment.