Update python FileAsset to accept os.PathLike in addition to str. (#3368)

This should fix #2896.
This commit is contained in:
Ryan Campbell 2019-10-18 15:31:59 -06:00 committed by Pat Gavlin
parent fa20d88e12
commit 665b4caa89
4 changed files with 14 additions and 6 deletions

View file

@ -3,6 +3,9 @@ CHANGELOG
## HEAD (Unreleased) ## HEAD (Unreleased)
- `FileAsset` in the Python SDK now accepts anything implementing `os.PathLike` in addition to `str`.
[#3368](https://github.com/pulumi/pulumi/pull/3368)
## 1.3.4 (2019-10-18) ## 1.3.4 (2019-10-18)
- Remove unintentional console outupt introduced in 1.3.3. - Remove unintentional console outupt introduced in 1.3.3.

View file

@ -15,6 +15,7 @@
""" """
Assets are the Pulumi notion of data blobs that can be passed to resources. Assets are the Pulumi notion of data blobs that can be passed to resources.
""" """
from os import PathLike, fspath
from typing import Dict, Union from typing import Dict, Union
from .runtime import known_types from .runtime import known_types
@ -36,10 +37,11 @@ class FileAsset(Asset):
A FileAsset is a kind of asset produced from a given path to a file on A FileAsset is a kind of asset produced from a given path to a file on
the local filesysetm. the local filesysetm.
""" """
def __init__(self, path: str) -> None:
if not isinstance(path, str): def __init__(self, path: Union[str, PathLike]) -> None:
raise TypeError("FileAsset path must be a string") if not isinstance(path, (str, PathLike)):
self.path = path raise TypeError("FileAsset path must be a string or os.PathLike")
self.path = fspath(path)
@known_types.string_asset @known_types.string_asset

View file

@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from pathlib import Path
from pulumi import CustomResource from pulumi import CustomResource
from pulumi.asset import FileAsset, StringAsset, RemoteAsset from pulumi.asset import FileAsset, StringAsset, RemoteAsset
@ -23,5 +25,6 @@ class MyResource(CustomResource):
}) })
MyResource("file", FileAsset("./testfile.txt")) MyResource("file", FileAsset("./testfile.txt"))
MyResource("file", FileAsset(Path(".") / "testfile.txt"))
MyResource("string", StringAsset("its a string")) MyResource("string", StringAsset("its a string"))
MyResource("remote", RemoteAsset("https://pulumi.com")) MyResource("remote", RemoteAsset("https://pulumi.com"))

View file

@ -21,7 +21,7 @@ class AssetTest(LanghostTest):
def test_asset(self): def test_asset(self):
self.run_test( self.run_test(
program=path.join(self.base_path(), "asset"), program=path.join(self.base_path(), "asset"),
expected_resource_count=3) expected_resource_count=4)
def register_resource(self, _ctx, _dry_run, ty, name, resource, def register_resource(self, _ctx, _dry_run, ty, name, resource,
_dependencies, _parent, _custom, _protect, _provider, _property_deps, _delete_before_replace, _dependencies, _parent, _custom, _protect, _provider, _property_deps, _delete_before_replace,
@ -29,7 +29,7 @@ class AssetTest(LanghostTest):
self.assertEqual(ty, "test:index:MyResource") self.assertEqual(ty, "test:index:MyResource")
if name == "file": if name == "file":
self.assertIsInstance(resource["asset"], FileAsset) self.assertIsInstance(resource["asset"], FileAsset)
self.assertEqual(resource["asset"].path, "./testfile.txt") self.assertEqual(path.normpath(resource["asset"].path), "testfile.txt")
elif name == "string": elif name == "string":
self.assertIsInstance(resource["asset"], StringAsset) self.assertIsInstance(resource["asset"], StringAsset)
self.assertEqual(resource["asset"].text, "its a string") self.assertEqual(resource["asset"].text, "its a string")