Fix ObjectDisposedException thrown when calling Yaml.LoadAllFromFileAsync (#573)
This commit is contained in:
committed by
GitHub
parent
b282b9571b
commit
a4350d6c8f
@@ -80,11 +80,11 @@ namespace k8s
|
||||
/// <param name="fileName">The name of the file to load from.</param>
|
||||
/// <param name="typeMap">A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod)</param>
|
||||
/// <returns>collection of objects</returns>
|
||||
public static Task<List<object>> LoadAllFromFileAsync(string fileName, Dictionary<string, Type> typeMap)
|
||||
public static async Task<List<object>> LoadAllFromFileAsync(string fileName, Dictionary<string, Type> typeMap)
|
||||
{
|
||||
using (var reader = File.OpenRead(fileName))
|
||||
using (var fileStream = File.OpenRead(fileName))
|
||||
{
|
||||
return LoadAllFromStreamAsync(reader, typeMap);
|
||||
return await LoadAllFromStreamAsync(fileStream, typeMap).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using k8s.Models;
|
||||
using Xunit;
|
||||
|
||||
@@ -34,6 +35,44 @@ metadata:
|
||||
Assert.Equal("ns", ((V1Namespace)objs[1]).Metadata.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadAllFromFile()
|
||||
{
|
||||
var content = @"apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: ns";
|
||||
|
||||
var types = new Dictionary<string, Type>();
|
||||
types.Add("v1/Pod", typeof(V1Pod));
|
||||
types.Add("v1/Namespace", typeof(V1Namespace));
|
||||
|
||||
var tempFileName = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
|
||||
|
||||
var objs = await Yaml.LoadAllFromFileAsync(tempFileName, types).ConfigureAwait(false);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<V1Pod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
Assert.Equal("foo", ((V1Pod)objs[0]).Metadata.Name);
|
||||
Assert.Equal("ns", ((V1Namespace)objs[1]).Metadata.Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFileName))
|
||||
{
|
||||
File.Delete(tempFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadFromString()
|
||||
{
|
||||
@@ -107,6 +146,32 @@ metadata:
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LoadFromFile()
|
||||
{
|
||||
var content = @"apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: foo
|
||||
";
|
||||
|
||||
var tempFileName = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
|
||||
|
||||
var obj = await Yaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(false);
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(tempFileName))
|
||||
{
|
||||
File.Delete(tempFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteToString()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user