-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Args: Add
path
arg type to fix working directory handling (#442)
Summary: Pull Request resolved: #442 * Add new argument type `path:` * This makes relative paths work correctly when passed as arguments * Previously, if you passed `--arg myarg=foo/bar` the behavior was busted because the TTP will change its working directory to the parent directory of the TTP's YAML file, and `foo/bar` won't exist there, even if it existed in the directory from which you invoked ttpforge :( * Updated all examples to use the new arguments Reviewed By: d0n601 Differential Revision: D51475492 fbshipit-source-id: 4dea0a1b0415ab39580ace902e3bd6e75aab315e
- Loading branch information
1 parent
221a805
commit 246f1a2
Showing
10 changed files
with
281 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
cmd/test-resources/repos/test-repo/ttps/args/path/with-path.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Testing Path Type Arguments | ||
description: | | ||
This TTP powers the test case | ||
"Argument with `type: path` - Should Succeed" | ||
in `cmd/run_test.go` | ||
args: | ||
- name: target_path | ||
description: | | ||
If a relative path is provided for this argument, it will | ||
be expanded to an absolute path based on the user's current | ||
working directory, NOT the configuration directory of the TTP. | ||
type: path | ||
steps: | ||
- name: cat_target_path | ||
inline: cat {{.Args.target_path}} |
14 changes: 14 additions & 0 deletions
14
cmd/test-resources/repos/test-repo/ttps/args/path/without-path.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: Testing Path Type Arguments - Failure | ||
description: | | ||
This TTP powers the test case | ||
"Argument with `type: path` - Should Fail" | ||
in `cmd/run_test.go` | ||
args: | ||
- name: target_path | ||
description: | | ||
If a valid relative path is provided for this argument, the TTP | ||
will still fail because it will run from the TTP's configuration | ||
directory and that path won't exist there | ||
steps: | ||
- name: cat_target_path | ||
inline: cat {{.Args.target_path}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright © 2023-present, Meta Platforms, Inc. and affiliates | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
package args | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateArgsPath(t *testing.T) { | ||
|
||
// used in some test cases | ||
homedir, err := os.UserHomeDir() | ||
require.NoError(t, err) | ||
|
||
// cd into this directory and use it to verify | ||
// that relative paths are expanded appropriately | ||
tmpDir, err := os.MkdirTemp("", "ttpforge-testing") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmpDir) | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
err = os.Chdir(tmpDir) | ||
require.NoError(t, err) | ||
defer func() { | ||
// this will break a bunch of other tests so be loud about it | ||
if err := os.Chdir(wd); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
// macOS /private/var/... shenanigans | ||
tmpDir, err = filepath.EvalSymlinks(tmpDir) | ||
require.NoError(t, err) | ||
|
||
testCases := []validateTestCase{ | ||
{ | ||
name: "Absolute Path", | ||
specs: []Spec{ | ||
{ | ||
Name: "mypath", | ||
Type: "path", | ||
}, | ||
}, | ||
argKvStrs: []string{ | ||
"mypath=/tmp/foo", | ||
}, | ||
expectedResult: map[string]any{ | ||
"mypath": "/tmp/foo", | ||
}, | ||
}, | ||
{ | ||
name: "Path Containing Tilde", | ||
specs: []Spec{ | ||
{ | ||
Name: "mypath", | ||
Type: "path", | ||
}, | ||
}, | ||
argKvStrs: []string{ | ||
"mypath=~/wut", | ||
}, | ||
expectedResult: map[string]any{ | ||
"mypath": filepath.Join(homedir, "wut"), | ||
}, | ||
}, | ||
{ | ||
name: "Relative Path", | ||
specs: []Spec{ | ||
{ | ||
Name: "mypath", | ||
Type: "path", | ||
}, | ||
}, | ||
argKvStrs: []string{ | ||
"mypath=" + filepath.Join("foo", "bar"), | ||
}, | ||
// can't do a strict equality 0 | ||
expectedResult: map[string]any{ | ||
"mypath": filepath.Join(tmpDir, "foo", "bar"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
checkValidateTestCase(t, tc) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters