Repository: JanDeDobbeleer/oh-my-posh
Pull request #991 (2021-09-24 05:30:47)

fix(pwsh): validate correct AZ context object
### Prerequisites

- [ ] I have read and understood the `CONTRIBUTING` guide
- [ ] The commit message follows the [conventional commits][cc] guidelines
- [ ] Tests for the changes have been added (for bug fixes/features)
- [ ] Docs have been added / updated (for bug fixes/features)

### Description

[Description of the change]

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary


lnu: I noticed the casing of const is not the same in all places. Sometimes it's all uppercase, sometimes PascalCase. You started with PascalCase I think, right?

Patch:
diff --git a/src/init/omp.ps1 b/src/init/omp.ps1
index 843ba237..0f227805 100644
--- a/src/init/omp.ps1
+++ b/src/init/omp.ps1
@@ -58,7 +58,7 @@ function global:Initialize-ModuleSupport {
     if ($env:AZ_ENABLED -eq $true) {
         try {
             $context = Get-AzContext
-            if ($null -ne $subscription) {
+            if ($null -ne $context) {
                 $env:AZ_ENVIRONMENT_NAME = $context.Environment.Name
                 $env:AZ_USER_NAME = $context.Account.Id
                 $env:AZ_SUBSCRIPTION_ID = $context.Subscription.Id
diff --git a/src/segment_owm.go b/src/segment_owm.go
index 3adef037..03842647 100644
--- a/src/segment_owm.go
+++ b/src/segment_owm.go
@@ -15,16 +15,16 @@ type owm struct {
 }
 
 const (
-	// APIKEY openweathermap api key
-	APIKEY Property = "apikey"
-	// LOCATION openweathermap location
-	LOCATION Property = "location"
-	// UNITS openweathermap units
-	UNITS Property = "units"
-	// CACHETimeout cache timeout
-	CACHETimeout Property = "cache_timeout"
-	// CACHEKey key used when caching the response
-	CACHEKey string = "owm_response"
+	// APIKey openweathermap api key
+	APIKey Property = "apikey"
+	// Location openweathermap location
+	Location Property = "location"
+	// Units openweathermap units
+	Units Property = "units"
+	// CacheTimeout cache timeout
+	CacheTimeout Property = "cache_timeout"
+	// CacheKey key used when caching the response
+	CacheKey string = "owm_response"
 )
 
 type weather struct {
@@ -66,50 +66,45 @@ func (d *owm) string() string {
 }
 
 func (d *owm) getResult() (*OWMDataResponse, error) {
-	apikey := d.props.getString(APIKEY, ".")
-	location := d.props.getString(LOCATION, "De Bilt,NL")
-	units := d.props.getString(UNITS, "standard")
-	httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
-	cacheTimeout := int64(d.props.getInt(CACHETimeout, DefaultCacheTimeout))
-
-	d.url = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey)
-
+	cacheTimeout := int64(d.props.getInt(CacheTimeout, DefaultCacheTimeout))
+	response := new(OWMDataResponse)
 	if cacheTimeout > 0 {
 		// check if data stored in cache
-		val, found := d.env.cache().get(CACHEKey)
+		val, found := d.env.cache().get(CacheKey)
 		// we got something from te cache
 		if found {
-			q := new(OWMDataResponse)
-			err := json.Unmarshal([]byte(val), q)
+			err := json.Unmarshal([]byte(val), response)
 			if err != nil {
 				return nil, err
 			}
-			return q, nil
+			return response, nil
 		}
 	}
 
+	apikey := d.props.getString(APIKey, ".")
+	location := d.props.getString(Location, "De Bilt,NL")
+	units := d.props.getString(Units, "standard")
+	httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
+	d.url = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey)
+
 	body, err := d.env.doGet(d.url, httpTimeout)
 	if err != nil {
 		return new(OWMDataResponse), err
 	}
-	q := new(OWMDataResponse)
-	err = json.Unmarshal(body, &q)
+	err = json.Unmarshal(body, &response)
 	if err != nil {
 		return new(OWMDataResponse), err
 	}
 
 	if cacheTimeout > 0 {
 		// persist new forecasts in cache
-		d.env.cache().set(CACHEKey, string(body), cacheTimeout)
-		if err != nil {
-			return nil, err
-		}
+		d.env.cache().set(CacheKey, string(body), cacheTimeout)
 	}
-	return q, nil
+	return response, nil
 }
 
 func (d *owm) setStatus() error {
-	units := d.props.getString(UNITS, "standard")
+	units := d.props.getString(Units, "standard")
 
 	q, err := d.getResult()
 	if err != nil {


Test patch:
diff --git a/src/segment_owm_test.go b/src/segment_owm_test.go
index faa2faf9..ccb17a66 100644
--- a/src/segment_owm_test.go
+++ b/src/segment_owm_test.go
@@ -38,10 +38,10 @@ func TestOWMSegmentSingle(t *testing.T) {
 		env := &MockedEnvironment{}
 		props := &properties{
 			values: map[Property]interface{}{
-				APIKEY:       "key",
-				LOCATION:     "AMSTERDAM,NL",
-				UNITS:        "metric",
-				CACHETimeout: 0,
+				APIKey:       "key",
+				Location:     "AMSTERDAM,NL",
+				Units:        "metric",
+				CacheTimeout: 0,
 			},
 		}
 
@@ -165,10 +165,10 @@ func TestOWMSegmentIcons(t *testing.T) {
 		env := &MockedEnvironment{}
 		props := &properties{
 			values: map[Property]interface{}{
-				APIKEY:       "key",
-				LOCATION:     "AMSTERDAM,NL",
-				UNITS:        "metric",
-				CACHETimeout: 0,
+				APIKey:       "key",
+				Location:     "AMSTERDAM,NL",
+				Units:        "metric",
+				CacheTimeout: 0,
 			},
 		}
 
@@ -194,9 +194,9 @@ func TestOWMSegmentFromCache(t *testing.T) {
 	cache := &MockedCache{}
 	props := &properties{
 		values: map[Property]interface{}{
-			APIKEY:   "key",
-			LOCATION: "AMSTERDAM,NL",
-			UNITS:    "metric",
+			APIKey:   "key",
+			Location: "AMSTERDAM,NL",
+			Units:    "metric",
 		},
 	}
 	o := &owm{
@@ -206,6 +206,7 @@ func TestOWMSegmentFromCache(t *testing.T) {
 	cache.On("get", "owm_response").Return(response, true)
 	cache.On("set").Return()
 	env.On("cache", nil).Return(cache)
+	// env.On("doGet", "http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key").Return([]byte(response), nil)
 
 	assert.Nil(t, o.setStatus())
 	assert.Equal(t, expectedString, o.string())