patch 8.0.1592: terminal windows in a session are not properly restored

Problem:    Terminal windows in a session are not properly restored.
Solution:   Add "terminal" in 'sessionoptions'.  When possible restore the
            command running in a terminal.
diff --git a/src/testdir/shared.vim b/src/testdir/shared.vim
index 5fef6bd..877c5f9 100644
--- a/src/testdir/shared.vim
+++ b/src/testdir/shared.vim
@@ -270,3 +270,10 @@
   let line = join(chars, '')
   return matchstr(line, '^.\{-}\ze\s*$')
 endfunc
+
+" Stops the shell running in terminal "buf".
+func Stop_shell_in_terminal(buf)
+  call term_sendkeys(a:buf, "exit\r")
+  let job = term_getjob(a:buf)
+  call WaitFor({-> job_status(job) == "dead"})
+endfunc
diff --git a/src/testdir/test_mksession.vim b/src/testdir/test_mksession.vim
index cdb6f4c..81883b5 100644
--- a/src/testdir/test_mksession.vim
+++ b/src/testdir/test_mksession.vim
@@ -7,6 +7,8 @@
   finish
 endif
 
+source shared.vim
+
 func Test_mksession()
   tabnew
   let wrap_save = &wrap
@@ -99,6 +101,7 @@
   call delete('Xtest_mks.out')
   call delete(tmpfile)
   let &wrap = wrap_save
+  set sessionoptions&
 endfunc
 
 func Test_mksession_winheight()
@@ -150,6 +153,107 @@
   call delete('Xtest_mks.out')
 endfunc
 
+if has('terminal')
+
+func Test_mksession_terminal_shell()
+  terminal
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      let term_cmd = line
+    elseif line =~ 'badd.*' . &shell
+      call assert_report('unexpected shell line: ' . line)
+    endif
+  endfor
+  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*$', term_cmd)
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+endfunc
+
+func Test_mksession_terminal_no_restore_cmdarg()
+  terminal ++norestore
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      call assert_report('session must not restore teminal')
+    endif
+  endfor
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+endfunc
+
+func Test_mksession_terminal_no_restore_funcarg()
+  call term_start(&shell, {'norestore': 1})
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      call assert_report('session must not restore teminal')
+    endif
+  endfor
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+endfunc
+
+func Test_mksession_terminal_no_restore_func()
+  terminal
+  call term_setrestore(bufnr('%'), 'NONE')
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      call assert_report('session must not restore teminal')
+    endif
+  endfor
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+endfunc
+
+func Test_mksession_terminal_no_ssop()
+  terminal
+  set sessionoptions-=terminal
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      call assert_report('session must not restore teminal')
+    endif
+  endfor
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+  set sessionoptions&
+endfunc
+
+func Test_mksession_terminal_restore_other()
+  terminal
+  call term_setrestore(bufnr('%'), 'other')
+  mksession! Xtest_mks.out
+  let lines = readfile('Xtest_mks.out')
+  let term_cmd = ''
+  for line in lines
+    if line =~ '^terminal'
+      let term_cmd = line
+    endif
+  endfor
+  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+ other', term_cmd)
+
+  call Stop_shell_in_terminal(bufnr('%'))
+  call delete('Xtest_mks.out')
+endfunc
+
+endif " has('terminal')
 
 
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_terminal.vim b/src/testdir/test_terminal.vim
index ac54bbc..0e04abd 100644
--- a/src/testdir/test_terminal.vim
+++ b/src/testdir/test_terminal.vim
@@ -30,13 +30,6 @@
   return buf
 endfunc
 
-" Stops the shell started by Run_shell_in_terminal().
-func Stop_shell_in_terminal(buf)
-  call term_sendkeys(a:buf, "exit\r")
-  call WaitFor('job_status(g:job) == "dead"')
-  call assert_equal('dead', job_status(g:job))
-endfunc
-
 func Test_terminal_basic()
   au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
   let buf = Run_shell_in_terminal({})