Skip to content
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

download: Infer track and exercise slug from CWD #880

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
d.apibaseurl = usrCfg.GetString("apibaseurl")
d.workspace = usrCfg.GetString("workspace")

if d.uuid == "" {
if d.slug == "" {
if _, slug, ok := trackAndSlugFromCwd(d.workspace); ok && slug != "" {
Copy link
Member Author

@petertseng petertseng Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, the != "" check is, strictly speaking, unnecessary. If we have gotten to this point, then d.slug is already "" and setting it from "" to "" does nothing.

d.slug = slug
}
}
if d.track == "" && d.team == "" {
if track, _, ok := trackAndSlugFromCwd(d.workspace); ok {
d.track = track
}
}
}

if err = d.needsSlugXorUUID(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -349,6 +362,46 @@ func (sf solutionFile) relativePath() string {
return filepath.FromSlash(file)
}

// trackAndSlugFromCwd infers track and slug from current working directory,
// by finding the two path components coming immediately after the workspace.
func trackAndSlugFromCwd(workspace string) (string, string, bool) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surely, it would be possible to call this flagsFromCwd and have it automatically detect the team if inside a teams directory (from CWD) and the UUID if inside users (but this requires reading a file in order to get the UUID). Since I don't use either of these pieces of functionality, and the latter needs more thought, I would not like to take on such a task in this PR.

petertseng marked this conversation as resolved.
Show resolved Hide resolved
cwd, err := os.Getwd()
if err != nil {
return "", "", false
}

if !strings.HasPrefix(cwd, workspace) {
return "", "", false
}

afterWorkspace := strings.TrimPrefix(cwd, workspace)
separator := string([]rune{os.PathSeparator})
afterWorkspace = strings.TrimPrefix(afterWorkspace, separator)

components := strings.Split(afterWorkspace, separator)
if len(components) == 0 {
return "", "", false
}

// We have at least one component, possibly more.
// First is presumably the track, but check whether it actually is one.
track := components[0]
if !isTrack(track) {
return "", "", false
}

slug := ""
if len(components) >= 2 {
slug = components[1]
}

return track, slug, true
}

func isTrack(dir string) bool {
return dir != "users" && dir != "teams"
}

func setupDownloadFlags(flags *pflag.FlagSet) {
flags.StringP("uuid", "u", "", "the solution UUID")
flags.StringP("track", "t", "", "the track ID")
Expand Down
73 changes: 73 additions & 0 deletions cmd/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,79 @@ func TestDownloadWithoutFlags(t *testing.T) {
}
}

func TestDownloadInferringFlags(t *testing.T) {
co := newCapturedOutput()
co.override()
defer co.reset()

originalCwd, err := os.Getwd()
defer os.Chdir(originalCwd)
assert.NoError(t, err)

testCases := []struct {
cwd string
flags map[string]string
ok bool
}{
{
cwd: "bogus-track",
flags: map[string]string{"exercise": "bogus-exercise"},
ok: true,
},
{
cwd: "bogus-track/bogus-exercise",
flags: nil,
ok: true,
},
{
cwd: "teams/bogus-team/bogus-track/bogus-exercise",
flags: nil,
ok: false,
},
{
cwd: "users/bogus-user/bogus-track/bogus-exercise",
flags: nil,
ok: false,
},
}

for _, tc := range testCases {
tmpDir, err := ioutil.TempDir("", "download-infer")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

subdir := filepath.Join(tmpDir, tc.cwd)
err = os.MkdirAll(subdir, 0755)
assert.NoError(t, err)
os.Chdir(subdir)

ts := fakeDownloadServer(strconv.FormatBool(true), "")
defer ts.Close()

v := viper.New()
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)
v.Set("token", "abc123")

cfg := config.Config{
UserViperConfig: v,
}
flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
setupDownloadFlags(flags)
for name, value := range tc.flags {
flags.Set(name, value)
}

err = runDownload(cfg, flags, []string{})
if tc.ok {
assert.NoError(t, err)
assertDownloadedCorrectFiles(t, tmpDir)
} else {
assert.Error(t, err)
}
}
}

func TestSolutionFile(t *testing.T) {
testCases := []struct {
name, file, expectedPath, expectedURL string
Expand Down